Switching to further part of code from conditional statement 'if'
1 次查看(过去 30 天)
显示 更早的评论
Suppose I have the following structure of code (below). The point is, in 2 different parts of my program I have exactly the same code, which is marked with the letter 'A'. Is there any way to avoid the repetition of the code A in conditional statement 'if' but instead, force the program to move to the further part of the program?
for n = 1:length(vectorOfNames)
% ...
try
% function that may return an error
catch
% error handling
if ~isempty(vectorOfNames2{n}) % if condition is True
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% A
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
else isempty(vectorOfNames2{n}) % if condition is False
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% B
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end
continue
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% A
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end
0 个评论
采纳的回答
更多回答(2 个)
Walter Roberson
2021-2-4
No. Use functions to avoid repeating code.
Your example could be written as
try
something
catch
if isempty(whatever)
B
end
end
A
0 个评论
Joanna Przeworska
2021-2-5
1 个评论
Walter Roberson
2021-2-5
Acondition = true;
try
something
catch
if isempty(whatever)
B
continue
end
Acondition = C;
end
if Acondition
A
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Entering Commands 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!