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

 채택된 답변

KL
KL 2017년 11월 1일
편집: KL 2017년 11월 1일

0 개 추천

...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.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

질문:

2017년 11월 1일

댓글:

2017년 11월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by