Switching to further part of code from conditional statement 'if'
1 回表示 (過去 30 日間)
古いコメントを表示
Joanna Przeworska
2021 年 2 月 4 日
回答済み: Joanna Przeworska
2021 年 2 月 11 日
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!