for loop 実行中のエラー処理について

for i = 1:5
for loop 実行中に i = 2 でエラーが発生した場合、 i = 2 はエラーということで処理し、
i = 3 やその後も実行を継続していくためにはどうすればよいですか。

 Akzeptierte Antwort

Kojiro Saito
Kojiro Saito am 27 Okt. 2022

0 Stimmen

for文の中の処理をtry とcatch でくくれば可能です。
tryの中でエラーになっても処理が中断せず、catchの中でエラー処理をハンドルできます。
for n=1:5
try
disp(n)
if n==2
surf %入力引数無しでエラーとなる
end
catch ME
disp(ME) % Exceptionを表示
end
end
1 2
MException with properties: identifier: 'MATLAB:narginchk:notEnoughInputs' message: 'Not enough input arguments.' cause: {} stack: [4×1 struct] Correction: []
3 4 5

1 Kommentar

雅晶
雅晶 am 1 Nov. 2022
解決しました。ありがとうございます。

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu ループと条件付きステートメント finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2022b

Tags

Gefragt:

am 27 Okt. 2022

Kommentiert:

am 1 Nov. 2022

Community Treasure Hunt

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

Start Hunting!