Is it possible to change the upper end for the runtime variable of for loop during it's runtime?
Show older comments
The problem I have, is that I have a for-loop that iterates up to a certain number, but that number can increase during the runtime of the for-loop. As an example:
max = 10;
for i = 1:2:max
max = max + 1;
end
Is the value of i 10? Or is it more, i.e. does i run until it has caught up to max?
Thank you!
Accepted Answer
More Answers (1)
Dana Adkins
on 14 Jan 2014
1 vote
In short, “No.”
By just quickly running your example script the workspace variables end up being i=9 and max = 15 which would imply that while you can modify max however a copy of max’s value is used for the loop limit and not your newly modified max value.
This is not a practice I would recommend in any case.
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!