How to catch an error

1 回表示 (過去 30 日間)
John Miller
John Miller 2012 年 6 月 4 日
Hi,
if I get an error I want change the algorithm: like this:
if ERROR
ALGORITHM 2
else
ALGORITHM 1
end
Hope someone can help.
Thank YOu!

採用された回答

Image Analyst
Image Analyst 2012 年 6 月 4 日
Try it like this:
try
% No error yet, so try to run algorithm 1
algorithm1();
catch ME
% You get here if algorithm 1 bombs.
% Create an informative error message.
errorMessage = sprintf('An error occurred in function blah_fubar(). The error reported by MATLAB is:\n\n%s\nClick OK to run algorithm2()', ME.message);
% Print error message to command window.
fprintf('%s', errorMessage);
% Alert the user via a popup message.
uiwait(warndlg((errorMessage));
% Now run algorithm 2, because we had the error occur.
algorithm2();
end
  1 件のコメント
John Miller
John Miller 2012 年 6 月 4 日
Waoow nice ! :) This is perfect
THANK YOU VERY MUCH FOR YOUR HELP!!

サインインしてコメントする。

その他の回答 (1 件)

the cyclist
the cyclist 2012 年 6 月 4 日
You need the try-catch construct. Look up
>> doc try

カテゴリ

Help Center および File ExchangeIntroduction to Installation and Licensing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by