Returning to a particular line in my script
10 ビュー (過去 30 日間)
古いコメントを表示
I have an if condition(#3) inside a while loop(#4) that is all together inside another if condition(#1 & 2). if condition 3 is not satisfied, I'd like to divert my code to the second condition of my big if (*) How can I do this?
if condition1
while
...
if condition3
...[go to elseif ~condition1]
end
...
end
elseif ~condition2 *
...
end
0 件のコメント
回答 (3 件)
Image Analyst
2012 年 6 月 24 日
Here's one way. Break the else into it's own separate if, and set a flag for whether or not you need to go inside that if.
testForCondition2 = false;
if condition1
while
...
if condition3
...[go to if ~condition2]
testForCondition2 = true;
end
...
end
end
if testForCondition2 && ~condition2 *
...
end
0 件のコメント
Image Analyst
2012 年 6 月 24 日
if condition1
elseif ~condition1
end
is the same as
if condition1
else
end
The code I gave you will let you go into the else block only when you've checked the condition 3. It works because the else is further down the code. But to go from inside the else block back up to the original if line - well that seems weird to me with this type of construct. There is no "goto" in MATLAB so you can't do that. I suggest you rethink this and try to recode this in a different way, like say maybe using functions.
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!