You can turn that warning
into an error
, and errors can be caught by try/catch
blocks.
An example:
% Change this particular warning into an error
warnId = 'MATLAB:ode45:IntegrationTolNotMet';
warnstate = warning('error', warnId);
% That can be caught with try/catch
try
% Quick-failing integration, suggested by horchler
[t,y] = ode45(@(t,y)[y(1)^2;y(2)],[0 1],[1;1]);
catch ME
% Check if we indeed failed on meeting the tolerances
if strcmp(ME.identifier, warnId)
% DO YOUR STUFF HERE
else
% Something else has gone wrong: just re-throw the error
throw(ME);
end
end
% Don't forget to reset the warning status
warning(warnstate);
You can get the warnId
of any warning by virtue of the lastwarn
command. For errors, see lasterr
.