MATLAB: How do i resolve Subscript indices must either be real positive integers or logicals error?

Image Processing Toolboxlogicals error??must either be real positive integers or logicals error??subscript indices

rgb = imread('face2.jpg');
figure
imshow(rgb)
gray_image = rgb2gray(rgb);
imshow(gray_image);
[centers, radii] = imfindcircles(rgb,[6 20],'ObjectPolarity','dark')
imshow(rgb);
% h = viscircles(centers,radii);
l=length(radii);
e=0.5 %error
for i=0:l
if (radii(i)<=radii(i+1)+e & radii(i)>=radii(i+1)-e)
radii(1)=radii(i);
radii(2)=radii(i+1);
display(e)
end
end

Best Answer

You have
for i=0:l
if (radii(i)<=radii(i+1)+e & radii(i)>=radii(i+1)-e)
On the first iteration, i would be 0, so the if would try to be
if (radii(0)<=radii(0+1)+e & radii(0)>=radii(0+1)-e)
This is a problem because MATLAB indexing starts from 1, not from 0.
Your loop should probably be
for i = 1:l-1
I firmly recommend that you do not use l as a variable name, as it is difficult to tell apart from 1
Related Question