フィルターのクリア

continue or break a loop from a function

14 ビュー (過去 30 日間)
Ziad Sliti
Ziad Sliti 2020 年 4 月 8 日
コメント済み: Mehmed Saad 2020 年 4 月 9 日
Hello,
Is it possible to continue a caller loop from the function called ?
Example :
The caller is :
for i =1:5
test(i)
end
And the function is :
function test(i)
if i == 5
evalin('caller','continue')
end
end
and the result is :
Error using test (line 3)
Error: A CONTINUE may only be used within a FOR or WHILE loop.
Error in Untitled3 (line 2)
test(i)

採用された回答

Mehmed Saad
Mehmed Saad 2020 年 4 月 8 日
編集済み: Mehmed Saad 2020 年 4 月 8 日
You can signal that using flags
for i =1:5
flag= test(i);
if(flag)
continue;
end
end
And the function is :
function flag = test(i)
if i == 5
flag = true;
else
flag = false;
end
end
  2 件のコメント
Ziad Sliti
Ziad Sliti 2020 年 4 月 8 日
Yes thank you for your response, it's what I already did but I wanted to know if there was a "better" solution whitout creating a flag, and also to understand why "evalin" doesn't work.
Mehmed Saad
Mehmed Saad 2020 年 4 月 9 日
Try just runing a for loop, and enter a break point at iteration 3
Now Try typing break or continue on command prompt, it will give error
Error: A BREAK statement appeared outside of a loop. Use RETURN instead.
because you are not inside for loop due to breakpoint
Similar is the issue with eval or evalin function as when the evaluate you expression, it is not inside the for loop

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeIntroduction to Installation and Licensing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by