"Bruno Luong" <firstname.lastname@example.org> wrote in message news:email@example.com... > I would like to catch a warning,
Usually "catch" is used in the context of an error, not a warning. Do you want MATLAB to stop executing and enter debug mode (or just stop executing that function, no debug mode) when a warning is issued or do you simply want to stop it from being displayed?
> but do not want MATLAB displaying the message on the screen. > > I read the doc of warning(), lastwarn(), try/catch, and still can't find a > way to do it. > > Can someone give a hint.
If you want to prevent the warning from displaying, use WARNING with the 'off' flag and the identifier of the warning you want to silence. [Don't do "warning off" on its own if you can avoid it; that's using a nuclear bomb to kill a fly.] If you want to enter debug mode when the warning is issued, use DBSTOP.
% begin stopOnWarning.m function stopOnWarning
dbstop if warning MATLAB:plot:IgnoreImaginaryXYPart
x = 1:10; y = sin(x)+1i*cos(x); plot(x, y) z = x+y; % end stopOnWarning.m
If you wanted to stop execution if a particular warning is issued in a particular place, look at LASTWARN and then just RETURN if the warning you wanted to stop execution was issued.