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!