Info

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

Subscript indices must either be real positive integers or logicals.

1 view (last 30 days)
I am doing project on image processing, when i run my program i have the error appear in command window "Subscript indices must either be real positive integers or logicals."
The error in code-line is highlighted,
L = zeros(size(he));
for i=1:length(ROI)
rows = ROI(i,2)+[0:(ROI(i,4)-1)];
cols = ROI(i,1)+[0:(ROI(i,3)-1)];
rectMask = L(rows,cols);
spotMask = he(rows,cols);
rectMask(spotMask) = i;
L(rows,cols) = rectMask;
end
it's there any solution can solve it thank

Answers (1)

Guillaume
Guillaume on 16 Apr 2015
It would have been really helpful if you'd copied the whole of the error message. Particularly, the bit that shows the line responsible for the error.
My best guess is that it is rectMask(spotMask) = i line.
Again, I'm guessing that spotmask and he are supposed to be logical matrices of 0 and 1. So one possible cause for the error is that they are double matrices of 0 and 1. In that case, the following would solve the issue:
spotMask = logical(he(rows, cols)); %or just change he to logical before the loop
other possible reasons:
  • Some invalid values (negative or 0) in ROI
  • Some invalid (negative) values in he
There's also a bug waiting to bite you in your loop definition. length(ROI) will be the number of ROIs if you have 4 or more ROIs, but will be 4 (the number of columns) if you have 3 or less ROIs. That's because length returns the size of the largest dimension. Always be explicit about which dimension you want, and use size instead:
for roidx = 1:size(ROI, 1) %and avoid using i as a variable

Community Treasure Hunt

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

Start Hunting!