Index in position 1 is invalid.

2 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.date}.'); 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)));
[x,y]=find(Array==maximum);
%Apped the line of the maximum value in a new array
if ~isempty(Coordination)
Coordination(i,1:2) = Array(x,2:3);
end
clear stat
clc
end
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
G A on 9 Jul 2021
i as an index is not defined in your code, Matlab considers it as 1i by default
  4 Comments
ennes mulla
ennes mulla on 9 Jul 2021
Great! Thanks A lot!
I have also added the following line before 'end' of the for loop and it helped too
i = i+1,
But your helped me too. Thank you!
G A
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)

Categories

Find more on Images in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!