Warning: Integer operands are required for colon operator when used as index

For this code:
40: y1_x=y_data(1)
41: y1=round(y1_x)
42: y2_x=y_data(2)
43: y2=y2_x-y1_x+y1
I get this output(and warning):
y1_x =
57.3517
y1 =
57
y2_x =
83.3517
y2 =
83
Warning: Integer operands are required for colon operator when used as
index
> In sonar_prob at 43
In robot_diferential at 60
In Painel>RunSimulation at 259
In Painel>pushbuttonReactive_Callback at 154
In gui_mainfcn at 96
In Painel at 16
In @(hObject,eventdata)Painel('pushbuttonReactive_Callback',hObject,eventdata,guidata(hObject))
I couldn't find any reason for this warning...what can it mean?

1 Comment

Does the warning really happen on line 43? I know sometimes the Matlab warning or error message is misleading. Put a break point around line 43 and step through to see when exactly the warning happened.

Sign in to comment.

 Accepted Answer

This is caused by something like:
T = rand(1,10);
T(2.2:7.99) % Note the indexing by: 2.2:7.99
So you need to look through your code and spot where you are using a colon operator like the above. BTW, this fixes the warning message and produces the same output.
T(floor(2.2:7.99))
%
%
%
%
%
EDIT In response to your comments below:
Do this at the command line:
dbstop if warning
Then run the code again. The debugger will stop on the line that causes the warning...

10 Comments

Your example will give the error at the line "T(2.2:7.99)...".
But at my code the is no indexing operation at that line. I'm sure the code is saved and that I'm running the correct version of the file!
Regards,
Nuno
No, my example does not give an error at that line. A warning is not an error. Look below, pasted from the command line.
>> T = rand(1,10);
>> T(2.2:7.99)
Warning: Integer operands are required for colon operator when used as index
ans =
0.7792 0.9340 0.1299 0.5688 0.4694 0.0119
Now, I suggest you enter this at the command line:
dbstop if warning
Then run the code again. The debugger will stop on the line that caused the warning...
Sorry, I called error to the warning.
With "dbstop if warning" now I get the warning on the correct line. Let's start debugging :)
Is there any logic reason for the warning to appear on an incorrect line or there is simply no assurance that it will signal the exact line?
Good question! I don't know the answer. Was the warning at least in the same file as reported?
Yes. 14 lines after on this "example".
Perhaps a bug report is in order. I have seen this issue myself, but I don't have an example anymore. If you submit a bug report, TMW will look at your code and see what went wrong. At least calling DBSTOP gets it right!
The wrong line can get reported if you have modified the file since it was last saved, Or if the file is being invoked through a handle that was created before the latest modification was made.
Good thinking Walter! However, I have experienced the same issue when those two conditions weren't in play.
Hmmm. An old "object" would be an example of something that could have a handle to old code, I suspect.
Are you talking of some kind of cache? My files are saved, I'm sure!

Sign in to comment.

More Answers (1)

This warning normally occurs when doing something like:
>> y = x(index)
and the indexing vector has something other than a positive integer as an element.
I don't see how this could happen on the line of code you posted, so I guessing maybe the code was edited and not saved before you ran it. (Look for a star after the file name, in the editor.) Maybe the error really happened on line 41?
Try saving the code and trying again. Also, use breakpoints as suggested by Fangjun Jiang.

1 Comment

If the indexing vector has something other than a positive integer, an error will result, not a warning. MATLAB is smart, and when an index is created with the colon operator the results are floored before indexing. For example:
T = rand(1,10);
T(2.2:7.99) % A warning
idx = 2.2:7.99; % Now do this without the context.
T(idx) % An error.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 24 May 2011

Community Treasure Hunt

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

Start Hunting!