Error using sub2ind and error in imline/createMask ?

1 view (last 30 days)
Hi, while working with the below script I keep getting error message: Error using sub2ind (line 52) Out of range subscript. Error in imline/createMask (line 171) ind = sub2ind([m n], y, x); please can anyone help me with these error messages

Accepted Answer

Image Analyst
Image Analyst on 21 Jul 2015
sub2ind() does not appear in your attached code. So all I can suggest is that either m (the row) is greater than y (the number of rows), or n (the column) is greater than x (the total number of columns).
Another common problem is mixing up row,column with x,y.
  4 Comments
Image Analyst
Image Analyst on 23 Jul 2015
The code seems to have vanished. When an improper condition appears, it's always a judgment call by the Mathworks to try to fix the problem and try to recover, or to throw an error. I wonder what he did to get line endpoints outside the image. The function plot() will take points outside the image, but I guess they decided that createMask() should throw an error to let you know something's wrong. I bet they'd disagree about calling that behavior a bug.
Guillaume
Guillaume on 24 Jul 2015
I consider it a bug in the sense that: a) the documentation never states that the endpoints should be within the image, and b) the error you get does not hint at all at what the problem may be.
I have actually reported it to mathworks and they "will consider changing it in a future release".
Note that it's fairly trivial to get endpoints outside the image, simply using the interactive tool.

Sign in to comment.

More Answers (1)

Guillaume
Guillaume on 21 Jul 2015
The error message is indeed not very helpful, but the error is simply due to the fact that one or both endpoints of your line generated by imline are outside the boundaries of the image.
I would suggest you use the debugger to step through your code and find out why.
But first, fix the indenting of your file (right-click in the editor and select smart indent or just press CTRL+I). Once you've done that it'll be fairly obvious that your hline = imline(... and subsequent lines should be inside the for nn = ... loop. (or you've got a major conceptual failure in your code as, as it is, only the x1, x2, index1, etc. of the last iteration of the three loop will be used).

Community Treasure Hunt

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

Start Hunting!