Switching to further part of code from conditional statement 'if'

1 回表示 (過去 30 日間)
Joanna Przeworska
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

採用された回答

Joanna Przeworska
Joanna Przeworska 2021 年 2 月 11 日
Dear Walter,
Thank you for this solution. This is exactly what I was looking for.
Kind regards,
Joanna

その他の回答 (2 件)

Walter Roberson
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

Joanna Przeworska
Joanna Przeworska 2021 年 2 月 5 日
Yes, this solution could work, however I forgot to add one piece of code, which is in conditional statement before the part of code 'A' and not repeted further... I'm sorry. With this change, the code looks like the one below and I'm afraid I can't handle it in a simple way..
for n = 1:length(vectorOfNames)
% ...
try
% function that may return an error
catch
% error handling
if ~isempty(vectorOfNames2{n}) % if condition is True
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% C
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% A
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
else isempty(vectorOfNames2{n}) % if condition is False
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% B
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end
continue
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% A
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end
  1 件のコメント
Walter Roberson
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 ExchangeEntering Commands についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by