警告発生時,try/catchのような例外処理

조회 수: 18 (최근 30일)
Soichiro Nakano
Soichiro Nakano 2018년 10월 12일
댓글: Soichiro Nakano 2018년 10월 17일
try/catch~ ではエラー(プログラム停止相当)に対して,例外処理ができますが, 同様に警告(プログラム続行相当)に対して例外処理を行いたいときはどうすればよいのでしょうか.//(例えば,警告発生時,プログラムを停止する.)
  댓글 수: 1
Hirokazu Tanaka
Hirokazu Tanaka 2018년 10월 13일
勝手ながら sample.m の中身を下記に転記します。
result = 0;
%result = 2としたい
try
warning('warnig!!') %error('msg')ではない
result = 1
catch
result = 2
end

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

채택된 답변

Hirokazu Tanaka
Hirokazu Tanaka 2018년 10월 13일
"try catch warning MATLAB" で検索するとある程度ヒントになりそうなページが見つかりますが、
が参考になりそうです。自動翻訳機能で意味が取りにくいかもしれませんが、その場合は自動翻訳機能をオフにするか、元の言語を表示させてみてください。
try/catch ではないですが、Camさんの回答にある lastwarn を使った方法は面白いですね。参考まで転記します。
for ...
warning('') % Clear last warning message
... run code ...
[warnMsg, warnId] = lastwarn;
if ~isempty(warnMsg)
...react to warning...
end
end
  댓글 수: 1
Soichiro Nakano
Soichiro Nakano 2018년 10월 17일
ありがとうごさいます。確かにこれだと警告に対して分岐できますね。参考にさせていただきます。

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 デバッグと解析에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!