Can't change the loop index variable that controls the loop inside it -- see the following demo--
>> for i=1:4,disp(['before mod: ' num2str(i)]),i=i+pi;disp(['after mod: ' num2str(i)]),end
before mod: 1
after mod: 4.1416
before mod: 2
after mod: 5.1416
before mod: 3
after mod: 6.1416
before mod: 4
after mod: 7.1416
>>
Note that despite futzing with i, when the loop started over the next iteration, Matlab reset it back to the proper next value in the loop and terminated the loop after the four iterations. This is owing to the fact that the loop conditions are evaluated BEFORE the loop begins and stored independently from the local variable referenced inside the loop body.
If you need to have a more flexible looping arrangement that computes some differing looping construct other than a for loop. Several options would be available, one that might be reasonably simple would be to place your for loop in a while something like
whileFlag=true;
lp1=1; lp2=100;
while whileFlag
for i=lp1:lp2
if condition to change indices
lp1=i+23;
lp2=222;
break
end
end
if condition to quit entirely
whileFlag=false;
end
end
Another solution is to not use the loop index as the reference variable at all but to compute that variable internal to the loop. This has the advantage of setting that index arbitrarily but then the overall continuation of the loop isn't necessarily going to either keep going long enough or quit when expected.
The simplest solution would really depend on just precisely you needs are which aren't fully developed in the question.
Best Answer