How to skip error and continue (NOT in loop)?
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
If I have the following code, how to display x(10) after x(9999) causing an error?
x = ones(1, 10);
try
x(1)
x(9999)
x(10)
catch ME
fprintf(ME.message)
end
The output I got is:
ans =
1
Index exceeds the number of array elements (10).
Thanks!
採用された回答
Walter Roberson
2019 年 4 月 5 日
x = ones(1,10);
idx = [1 9999 10];
for K = 1 : length(idx)
try
x(K)
catch ME
fprintf(ME.message)
end
end
8 件のコメント
n33
2019 年 4 月 5 日
Thanks Walter, this works good.
So I assume if I have several user-defined functions, the only way to guarantee all functions get executed in a single run is to have several independent try-catch blocks or somehow put those functions in a loop?
%% GOOD
try
myfun1()
catch ME
end
try
myfun2()
catch ME
end
%% BAD
try
myfun1()
myfun2()
catch ME
end
funcs = {@myfun1, @myfun2};
for K = 1 : length(funcs)
try
funcs{K}();
catch ME
fprintf(ME.message);
end
end
Fernando
2023 年 5 月 11 日
What is the part of "not in a loop" you didn't understand?
Walter Roberson
2023 年 5 月 11 日
I'm not sure which part of "not in a loop" I did not understand. Perhaps you could explain?
Fernando
2023 年 5 月 11 日
if one has commands that truly cannot be done in a loop, then how can you skip a command error without having to try/catch every single command?
say:
A = command1thatcanpossiblycauseanerror;
B = command2thatcanpossiblycauseanerror;
C = command3thatcanpossiblycauseanerror;
D = command4thatcanpossiblycauseanerror;
E = command5thatcanpossiblycauseanerror;
F = command6thatcanpossiblycauseanerror;
G = command7thatcanpossiblycauseanerror;
H = command8thatcanpossiblycauseanerror;
I = command9thatcanpossiblycauseanerror;
J = command10thatcanpossiblycauseanerror;
K = command11thatcanpossiblycauseanerror;
L = command12thatcanpossiblycauseanerror;
M = command13thatcanpossiblycauseanerror;
N = command14thatcanpossiblycauseanerror;
O = command15thatcanpossiblycauseanerror;
P = command16thatcanpossiblycauseanerror;
Q = command17thatcanpossiblycauseanerror;
R = command18thatcanpossiblycauseanerror;
S = command19thatcanpossiblycauseanerror;
T = command20thatcanpossiblycauseanerror;
U = command21thatcanpossiblycauseanerror;
V = command22thatcanpossiblycauseanerror;
W = command23thatcanpossiblycauseanerror;
X = command24thatcanpossiblycauseanerror;
Y = command25thatcanpossiblycauseanerror;
Z = command26thatcanpossiblycauseanerror;
%where each commandXXthatcanpossiblycauseanerror is some command, really
%anything, something that cannot possibly be in a for loop.
%as the author asked.
Walter Roberson
2023 年 5 月 11 日
as the author asked
Sorry, could you explain to me where the author of the original question asked that?
Steven Lord
2023 年 5 月 11 日
A = command1thatcanpossiblycauseanerror;
B = command2thatcanpossiblycauseanerror;
C = command3thatcanpossiblycauseanerror;
D = command4thatcanpossiblycauseanerror;
% etc
That has a similar code smell to the frequently asked question about dynamically creating variables with numbered names like x1, x2, x3, etc., for which the general consensus is to avoid doing that.
Rather than posing a hypothetical scenario, can you share a little more detail about your actual application where you use a pattern like that code? What do those commands that can possibly throw an error do? Are they independent or are the outputs of one or more of those functions the inputs to later of those functions?
Walter Roberson
2023 年 5 月 11 日
I am having difficulty thinking of any code that is such that
A = command1thatcanpossiblycauseanerror;
is valid code, but
for dfpSzcGKKqyMbgIcvYXqkicEiRRKtIUpb_vjDshBQupYrSfhm_GS_xNidaex = 1 : 1
A = command1thatcanpossiblycauseanerror;
end
"cannot" work.
The only things I can come up with are along the lines of "needing" dbstack to be able to pinpoint the function name... ummm, no, even that would not explain things.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Whos についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
