How to catch an error

1 visualizzazione (ultimi 30 giorni)
John Miller
John Miller il 4 Giu 2012
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!

Risposta accettata

Image Analyst
Image Analyst il 4 Giu 2012
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 Commento
John Miller
John Miller il 4 Giu 2012
Waoow nice ! :) This is perfect
THANK YOU VERY MUCH FOR YOUR HELP!!

Accedi per commentare.

Più risposte (1)

the cyclist
the cyclist il 4 Giu 2012
You need the try-catch construct. Look up
>> doc try

Categorie

Scopri di più su Introduction to Installation and Licensing in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by