operator error for loop

1 view (last 30 days)
Engineer_guy
Engineer_guy on 13 Jul 2020
Commented: Stephen23 on 20 Oct 2020
I'm getting an opperator error on line 179. At this point I have a for loop where I say:
for idxGPS > 1 ;
Earlier in the code I declare idxGPS = 1. After that I have another for loop where I have data being stored in that variable so for this loop that original value of 1 should be overwritten if the previous loop processed correctly.
  2 Comments
Steven Lord
Steven Lord on 20 Oct 2020
Copying question in case it gets edited away or deleted as this user has done to several previous questions.
"I'm getting an opperator error on line 179. At this point I have a for loop where I say:
for idxGPS > 1 ;
Earlier in the code I declare idxGPS = 1. After that I have another for loop where I have data being stored in that variable so for this loop that original value of 1 should be overwritten if the previous loop processed correctly."

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 13 Jul 2020
In MATLAB, a for statement must have one of the following syntaxes:
  • for variable = scalar_value : scalar_value
  • for variable = scalar_value : scalar_value : scalar_value
  • for variable = scalar_value
  • for variable = nonscalar_value
In the above, variable_name must be a simple un-indexed name.
In the above, scalar_value and nonscalar_value can be expressions that evaluate to a scalar value or nonscalar value.
Basically, the syntax is really just
for variable = expression
except that the cases with : are optimized internally.
It would thus be valid to write
for control = idxGPS > 1
As your idxGPS is scalar numeric, idxGPS > 1 would be scalar, either 0 (false) or 1 (true), and that value would be assigned to the variable for one iteration.
If you wanted to only do the body once if the condition held, and for some reason you really had an urge to use a for instead of the more straight-forward if then you could do
for control = true : idxGPS > 1
That would do the body of the loop once and only in the case that idxGPS > 1
Your code is missing a bunch of "end" statements, and appears to have improper nesting.
Your code uses otherwise which is restricted for use in a switch() statement, but you are not using a switch statement.
If you have a hankering to execute exactly one of those sections in an obscure but legal way, then
switch true
case idxGPS > 1
stuff
case idxGLONASS > 1
stuff
otherwise
stuff
end
  7 Comments
Engineer_guy
Engineer_guy on 13 Jul 2020
Thank you !
Engineer_guy
Engineer_guy on 13 Jul 2020
I still have a while loop but I'm going to try and add counter that exits the loop.

Sign in to comment.

More Answers (0)

Categories

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

Community Treasure Hunt

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

Start Hunting!