Index in position 1 is invalid.

6 views (last 30 days)
ennes mulla
ennes mulla on 9 Jul 2021
Edited: G A on 9 Jul 2021
Good day,
I have made the following Script to sotre the coordinates of centroid of laser beam from images
directory = 'C:\Users\anask\Desktop\test\';
Coordination = zeros(7,3);
files = dir([directory '/*.jpg']);
[~,index] = sortrows({}.'); files = files(index); clear index;
for x = 1: numel(files)
A= imread([directory '/' files(x).name]);
%Convert the image ino binary image
Ibw = im2bw(A);
%Fill image regions and holes
Ibw = imfill(Ibw,'holes');
Ilabel = bwlabel(Ibw);
%finding the centroid
stat = regionprops(Ibw,'Area','Centroid');
%Creating new array that have Area, X-coordinate, Y-coordinate
area = cat(1,stat.Area);
Coor = cat(1,stat.Centroid);
%Combaining the results in one Array
Array = cat(2,area,Coor);
%Adding header
%header = {'Area','x','y'};
%Array = [header; num2cell(Array)];
%Finding the maximum value
maximum = max(max(Array(:,1)));
%Apped the line of the maximum value in a new array
if ~isempty(Coordination)
Coordination(i,1:2) = Array(x,2:3);
clear stat
And I am keep getting the following error
Index in position 1 is invalid. Array indices must be positive integers or logical values.
Error in Test (line 34)
Coordination(i,1:2) = Array(x,2:3);
Can someone help me please to fix this issue, I want to store the coordinates in the coordinate array. The coordinate are derived from the row of the maximum area in 'Array' array.
Many thanks in advanced

Accepted Answer

G A on 9 Jul 2021
i as an index is not defined in your code, Matlab considers it as 1i by default
G A on 9 Jul 2021
Edited: G A on 9 Jul 2021
Avoid using i or j as indices. Also, my code above is still not correct, but if you have only one value in Array which is equal to maximum, your ii = ii + 1; at the end of the loop with ii = 1; before the loop will do the job.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!