Warning: Integer operands are required for colon operator when used as index
Afficher commentaires plus anciens
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 commentaire
Fangjun Jiang
le 24 Mai 2011
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.
Réponse acceptée
Plus de réponses (1)
the cyclist
le 24 Mai 2011
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 commentaire
Matt Fig
le 24 Mai 2011
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.
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!