您可以将其warning转换为error,并且错误可以被try/catch块捕获。
一个例子:
% 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);
您可以通过该命令获得warnId任何警告。lastwarn有关错误,请参阅lasterr。