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

11 Ansichten (letzte 30 Tage)
Ken
Ken am 5 Okt. 2016
Kommentiert: Ken am 5 Okt. 2016
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 Kommentar
Steven Lord
Steven Lord am 5 Okt. 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

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Matthew Eicholtz
Matthew Eicholtz am 5 Okt. 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 Kommentar
Ken
Ken am 5 Okt. 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by