MATLAB Answers

0

Is it available to change the end of a loop for inside it?

Asked by Amine ELMOUATAMID on 30 Apr 2019
Latest activity Commented on by Amine ELMOUATAMID on 30 Apr 2019
ssss.PNG
ssssss.PNG
the size of my variable out changes inside the loop for so that's why I recalculate it inside it but the loop variable i stops in the first size of the variable even sizeOut changes.
can't the endval of the loop change ? or it can't ?

  4 Comments

Show 1 older comment
if I break out of the for loop it won't do all the work
I want it to stay until the new size
The break would be conditional upon the size of sizeOut. In other words,
if sizeOut(1,1) == something
break
end
I understand but sizeOut(1,1) maybe stay in the first value and maybe changes so I can't test it with something to break the loop.
otherways the while loop is better

Sign in to comment.

Products

1 Answer

Answer by Adam Danz
on 30 Apr 2019
Edited by Adam Danz
on 30 Apr 2019
 Accepted Answer

You want to use a while loop for this purpose.
It might look something like this (tough to tell without seeing more of your code)
c = 2; %counter
while c <= sizeOut(1,1)
% Do something
c = c+1;
end
Another good suggestion by Stephen Cobeldick is to add a conditional break to the for-loop that exits the loop when the sizeOut variable reaches a certain size.

  3 Comments

while loop works perfect
but why for loop endval can't change??
A for-loop is designed to be executed a fixed number of times and once you enter the loop you cannot change the number of expected iterations. The only options are to conditionally break to leave the loop early or to continue to skip an iteration.
A while loop, on the other hand, is designed to continually iterate until a condition is met (break and continue are also options).
thank you this is really helpfull

Sign in to comment.