Difficulty with FOR LOOP - catch 22 situation!!

1 次查看(过去 30 天)
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?????!!!!

回答(1 个)

Walter Roberson
Walter Roberson 2012-8-12
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

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by