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!
Best Answer
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.
If you take a look at the documentation, you can see thatcatch ME stores the exception (all error information) in the variableME. This can be useful if there are multiple errors that you expect to occur, so you can vary your response based on information like the error ID.
You will also need the exception if you want tothrow,rethrow orthrowascaller.
Best Answer