Error of subscript indices must either be real positive integer or logicals.

1 view (last 30 days)
Hai. I am an undergraduate student. I am trying to learn Matlab at my best. I am making real time object tracking using IP webcam, where to detect number of cars and its speed estimation. I managed to only do until number of cars detected sadly, but i keep getting error as seen at title, in line "ss(ceil(B(i))-50:ceil(B(i))+50,ceil(A(i))-50:ceil(A(i))+50,:)=1000". Please help me to fix the error. Alas, how can i estimate the speed too? Thanks and bless you, sir / madam.

Answers (1)

Neuropragmatist
Neuropragmatist on 6 Aug 2019
Run this command at the command line:
dbstop if error
When you get your error Matlab will point you to a specific line, go there and see what it says. It will have some assignment on it like this:
a(i) = something;
Judging by the error I'm guessing that in your code i will be either 0, inf or a decimal, none of which can be used to index into a matrix.
From looking at the code (I haven't tried to run it at all) my guess would be that the error is on the line:
ss(ceil(B(i))-50:ceil(B(i))+50,ceil(A(i))-50:ceil(A(i))+50,:)=1000;
I bet that ceil(B(i)) is less than 50, so you are getting a negative index, but the problem might be somewhere completely different.
  2 Comments
John D'Errico
John D'Errico on 6 Aug 2019
My guess is the same, that for SOME i, you have either A(i) or B(i) less than or equal to 50. When that happens, you have just created an index that is less than or equal to zero. That of course, causes MATLAB to get upset.
However, there is ONE other possibility that I might see, if that is not the case.
If you have created a VARIABLE named ceil, then for example, ceil(2.3) is not 3. Instead, MATLAB will then try to index the variable ceil that you created. Of course, then it will again fail.
ceil = 1:5;
>> ceil(2.3)
Array indices must be positive integers or logical values.
'ceil' appears to be both a function and a variable. If this is unintentional, use 'clear ceil' to remove the variable 'ceil' from the workspace.
We cannot know for sure what the real problem is, since we cannot run your code, nor should we. The answer is simple, as has been stated. Check the values of A(i), B(i) at failure time. As well, look to see if you have created a variable named ceil. This command will tell you if there is a ceil variable.
which ceil
Steven Lord
Steven Lord on 6 Aug 2019
To add to what Metioche and John have said, one aspect of your code that increases the confusion is that I don't find your variable names very descriptive and you reuse the same name in multiple places. First B is the output of a call to bwareaopen, then it becomes the output of a call to imfill, then it becomes a column of the second output from a call to step.
It may be a little more typing (though tab completion can help mitigate that) but I encourage you to use distinct descriptive names for your variables, especially those that are likely to have a long lifespan (be used / referenced on lines of code that are many lines apart.) This can also aid people (including yourself) reading this code in the future to understand its purpose more easily.
And finally, you create variables named a, b, c, and d (where you probably want to use less than or equal to <= or max rather than less than < since 0 is not a valid index in MATLAB, and perhaps similarly for >= or min rather than >) but then never use them, using functions of A and B instead.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!