Azzera filtri
Azzera filtri

Using i as an index within a loop in Matlab 2016a

1 visualizzazione (ultimi 30 giorni)
Is there a simple, global way to force Matlab 2016 to allow i to be used as a vector index within a loop statement that uses i as the loop index? I've been using i as a convenient index in for loops forever, and the simple assignment statement in the for statement was always sufficient to convert it from it's built in complex type to an integer. i.e.
for i=1:5,
y(i)=stuff;
end;
In Matlab 2016a, this code barfs because i retains its complex definition within the scope of the loop, so can't be used as the index into a vector within the loop. Adding an explicit assignment to an integer e.g. i=0, before the loop, or changing i to another variable name works, but I'd rather not modify every routine that uses i as an index within a loop.
  1 Commento
Steven Lord
Steven Lord il 5 Ott 2016
Please show the full, exact text of the error message you receive. The behavior you're describing shouldn't happen and doesn't happen when I use release R2016a or R2016b.
for i = 1:5
y(i) = 23;
end

Accedi per commentare.

Risposta accettata

Matthew Eicholtz
Matthew Eicholtz il 5 Ott 2016
I don't know what you mean by "this code barfs". I ran a simple for-loop in MATLAB R2016a with i as the index variable and experienced no barfing.
But, as an aside, because of the complex definition of i, I tend to prefer using ii instead of i in for-loops.
  1 Commento
Ken
Ken il 5 Ott 2016
My mistake, sorry to waste your time. This issue was reported by a customer using our code, and I mis-interpreted the symptom working over the phone. Turns out the problem was another difference in later Matlab versions that we were already aware of. Thanks for the thoughtful reply.

Accedi per commentare.

Più risposte (0)

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by