Discussion:
How to catch warnings using try catch
(too old to reply)
o***@gmail.com
2007-11-05 00:28:20 UTC
Permalink
Is it possible to catch warnings like "matrix is close to singular" in
M files? Thanks.
Walter Roberson
2007-11-05 00:34:03 UTC
Permalink
Post by o***@gmail.com
Is it possible to catch warnings like "matrix is close to singular" in
M files? Thanks.
No.

However, if you have places where you expect there might be
warnings, you can test lastwarn and use error(lastwarn) if
you want to force an error to be raised (to be caught by try/catch
block presumably.)
--
"History is a pile of debris" -- Laurie Anderson
Michael Wengler
2012-05-31 17:00:31 UTC
Permalink
It is now possible to catch warnings. I am using R2010b where this works, I don't know if it works on any earlier versions than that.

The following code
1) gets a directory listing of the current directory,
2) finds all files that fit a certain name pattern
3) changes the warning msgID MATLAB:DELETE:Permission into an 'error' instead of a warning
4) deletes files in a try-catch block, and catches the files that can't be deleted because of this warning (now treated as an error) condition.
5) finally reverts this msgID back to its old state.

Without setting the state of this msg_id to 'error' the catch does not catch it.

ls = dir;
Ils = find(strncmp('BatInfoDoc',{ls.name},length('BatInfoDoc')));
s = warning('error','MATLAB:DELETE:Permission');
if length(Ils)>3
for I=Ils(1:end-3)
try
delete(ls(I).name);
catch exception
fprintf('Can''t delete %s\n',ls(I).name);
end
end
end
warning(s);
Post by Walter Roberson
Post by o***@gmail.com
Is it possible to catch warnings like "matrix is close to singular" in
M files? Thanks.
No.
However, if you have places where you expect there might be
warnings, you can test lastwarn and use error(lastwarn) if
you want to force an error to be raised (to be caught by try/catch
block presumably.)
--
"History is a pile of debris" -- Laurie Anderson
Phil Goddard
2012-06-01 23:49:37 UTC
Permalink
Undocumented, but has been available since at least R2008a.

Phil.
Yair Altman
2012-07-11 22:27:25 UTC
Permalink
Post by Phil Goddard
Undocumented, but has been available since at least R2008a.
Phil.
Actually, as far back as R14SP3 (Matlab 7.1, 2005), possibly even earlier.

http://UndocumentedMatlab.com/blog/trapping-warnings-efficiently/

Yair Altman
http://UndocumentedMatlab.com
Kristin
2012-06-02 21:39:21 UTC
Permalink
Post by o***@gmail.com
Is it possible to catch warnings like "matrix is close to singular" in
M files? Thanks.
dbstop if error
before I run an m-file, and it stops at the place where the error occurred (much like KEYBOARD).
William
2013-10-24 16:29:07 UTC
Permalink
Post by o***@gmail.com
Is it possible to catch warnings like "matrix is close to singular" in
M files? Thanks.
Hello,

I think the simplest way to do this is using 'dbstop if warning'. the code will catch in the local workspace of the warning.
Marvin.Dragon
2015-04-26 21:12:23 UTC
Permalink
Post by o***@gmail.com
Is it possible to catch warnings like "matrix is close to singular" in
M files? Thanks.
dbstop if warning
paul gay
2016-03-15 10:31:04 UTC
Permalink
Post by o***@gmail.com
Is it possible to catch warnings like "matrix is close to singular" in
M files? Thanks.
You also can use lastwarn to check whether this "matrix is close to singular" has been sent
You can then re-initialize to the empty string by: lastwarn('')

[m,id]=error(lastwarn);
if strcmp(id,'MATLAB:nearlySingularMatrix')
disp(strcat('the following warning occurred:'))
end
lastwarn('');
Raghu Ramakrishnan
2016-06-09 03:40:03 UTC
Permalink
Post by paul gay
Post by o***@gmail.com
Is it possible to catch warnings like "matrix is close to singular" in
M files? Thanks.
You also can use lastwarn to check whether this "matrix is close to singular" has been sent
You can then re-initialize to the empty string by: lastwarn('')
[m,id]=error(lastwarn);
if strcmp(id,'MATLAB:nearlySingularMatrix')
disp(strcat('the following warning occurred:'))
end
lastwarn('');
It should be [m,id]=lastwarn();

Continue reading on narkive:
Loading...