Clear Filters
Clear Filters

iteration only runs once.

9 views (last 30 days)
Bert Taekels
Bert Taekels on 1 Nov 2017
Commented: Bert Taekels on 1 Nov 2017
i have a part of the code that calls other functions until a parameter is true. To have a safety i put in a counter that can break out of the iteration if too many iterations don't give a result. I must do something wrong, because after one iteration the process stops, even wile in the workspace the papameter is still 0. Here is the part of my code
counter = 0;
for realpositiondetected = true
(call other functions)
counter = counter+1;
if counter >= 25
disp 'real position was not found in 25 iterations'
return
end
end
I don't have much programming experience, so i might be an easy fix i'm overlooking
Thanks Bert

Accepted Answer

KL
KL on 1 Nov 2017
Edited: KL on 1 Nov 2017
...a part of the code that calls other functions until a parameter is true...
You probably want to use while loop and then you need to change the variable realpositiondetected to false and change it to true inside the loop based on the return values of all the other functions. For example,
realpositiondetected = false;
counter = 0;
while ~realpositiondetected
%call the other functions here
%check what you want to do is fullfilled
counter = counter+1;
if(someReturnValue == whatYouWant)
realpositiondetected = true;
break;
elseif(counter>=25)
disp('counter exceeds limit, breaking the loop!')
break;
end
end
See I have used break instead of return. Documentation says, If you call the function or script that contains return directly, there is no invoking function and MATLAB returns control to the command prompt.
Also, you can break your loop in case if your realpositiondetected becomes true.
So I'd advise to use break for your purpose. read more here.

More Answers (0)

Categories

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

Products

Community Treasure Hunt

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

Start Hunting!