Index in position 1 is invalid.

3 次查看(过去 30 天)
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

采纳的回答

G A
G A 2021-7-9
i as an index is not defined in your code, Matlab considers it as 1i by default
  4 个评论
ennes mulla
ennes mulla 2021-7-9
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 2021-7-9
编辑:G A 2021-7-9
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.

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by