Difficulty with FOR LOOP - catch 22 situation!!

1 view (last 30 days)
Mike
Mike on 12 Aug 2012
I am fairly new to Matlab and |'m sincerely hoping somebody knows a solution to my Matlab problem as at the moment it seems impossible.
I have created a 'for loop' which first calculates small increments of structural displacements and then returns a value for the corresponding structural resistance during each iteration. This resistance value is dependent upon the region of a resistance-displacement graph the calculated displacement is at. There are 3 regions separated by two markers on the displacement axis (i.e. x-axis of the graph) and these markers are defined at the start of the loop.
When a particular condition is met, I want my code to update the value of the boundaries and it appears to do this. However, as the original boundaries are still in place at the start of the loop, the code then re-updates the boundaries to their previous values instead of those defined at the end of the last iteration.
Is there a way around this or is this not possible in matlab?????!!!!

Answers (1)

Walter Roberson
Walter Roberson on 12 Aug 2012
When a "for" loop is encountered, all the values for the start, increment, and stop are recorded internally, and changes to any of those during the body of the loop will not have an effect on how the "for" loop operates.
If you need to be able to change the parameters of the loop, use a "while" loop instead.
for X = A : B : C
becomes
X = A;
while X < C
....
X = X + B;
end

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!