iteration only runs once.
9 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
KL
2017-11-1
编辑:KL
2017-11-1
...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 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!