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

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

 채택된 답변

Kojiro Saito
Kojiro Saito 2022년 10월 27일

0 개 추천

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

雅晶
雅晶 2022년 11월 1일
解決しました。ありがとうございます。

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 ループと条件付きステートメント에 대해 자세히 알아보기

제품

릴리스

R2022b

태그

질문:

2022년 10월 27일

댓글:

2022년 11월 1일

Community Treasure Hunt

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

Start Hunting!