Info

This question is closed. Reopen it to edit or answer.

??? Index exceeds matrix dimensions error in ploting data from a vector

1 view (last 30 days)
Greetings. I got this error "??? Index exceeds matrix dimensions." when I run the loop function below.
a ='C:\Users\Ace\Desktop\splash\';
A =dir( strcat (a, '*.bmp' ));
for icount = 1 : length( A )
%load the background
%icount = 1;
buf = strcat(a, A(icount).name);
im1 = imread (buf);
%imshow(im1)
%cut the temperature indicator at the right side
[y, x, z] = size(im1);
cutImg = im1(1:y , 1:320, :);
%figure, imshow(cutImg)
%cut the outter environment from the pool for an easier splash
%detection.
[y, x, z] = size(cutImg);
cutImg2 = cutImg( yBound:y , 1:x, : );
%figure, imshow(cutImg2)
[y, x, z] = size(cutImg2);
blackCount = 0;
%count number of black pixel
for i = 1:x
for j = 1:y
R = uint16( cutImg2(j, i, 1) );
G = uint16( cutImg2(j, i, 2) );
B = uint16( cutImg2(j, i, 3) );
if R == 0 && G == 0 && B == 0
blackCount = blackCount + 1 ;
end
end
end
%blackPixel percentage in an image
blackB4 = blackCount / ( y * x ) * 100;
% next image
buf = strcat(a, A(icount+1).name);
im1 = imread (buf);
%cut the temperature indicator at the right side
[y, x, z] = size(im1);
cutImg = im1(1:y , 1:320, :);
%figure, imshow(cutImg)
%cut the outter environment from the pool for an easier splash
%detection.
[y, x, z] = size(cutImg);
cutImg2 = cutImg( yBound:y , 1:x, : );
%figure, imshow(cutImg2)
[y, x, z] = size(cutImg2);
blackCount = 0;
%count number of black pixel
for i = 1:x
for j = 1:y
R = uint16( cutImg2(j, i, 1) );
G = uint16( cutImg2(j, i, 2) );
B = uint16( cutImg2(j, i, 3) );
if R == 0 && G == 0 && B == 0
blackCount = blackCount + 1 ;
end
end
end
%blackPixel percentage in an image
blackAf = blackCount / ( y * x ) * 100 ;
diff = blackAf - blackB4;
if icount == 1;
diffMax = [ diff ];
else
diffMax = [ diffMax ; diff ];
end
end
figure; plot( diffMax );
I suspect the error message comes from diffMax = [ diffMax ; diff ];
Or anyone can advise me? Or the error message comes from other part of the code? And the graph does not come out. Hope you can help me. Thanks.
  1 Comment
Sean de Wolski
Sean de Wolski on 26 May 2011
Please post the error message in its entirety. If this saves as a function it'll even tell you what line the error is on.
Also, don't name a variable 'diff' as this is a very useful built-in MATLAB function.

Answers (1)

Sean de Wolski
Sean de Wolski on 26 May 2011
The problem is that you're swapping y,x in your for-loops for blackcount.
You could of course vectorize the whole thing with:
blackcounts = sum(sum(all(~uint16(cutImg2),3)))

Community Treasure Hunt

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

Start Hunting!