Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to catch warnings using try catch

Subject: How to catch warnings using try catch

From: ouyang.jie@gmail.com

Date: 5 Nov, 2007 00:28:20

Message: 1 of 7

Is it possible to catch warnings like "matrix is close to singular" in
M files? Thanks.

Subject: How to catch warnings using try catch

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 5 Nov, 2007 00:34:03

Message: 2 of 7

In article <1194222500.648764.230940@57g2000hsv.googlegroups.com>,
 <ouyang.jie@gmail.com> wrote:
>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

Subject: How to catch warnings using try catch

From: Michael Wengler

Date: 31 May, 2012 17:00:31

Message: 3 of 7

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);




roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in message <fglodr$aln$1@canopus.cc.umanitoba.ca>...
> In article <1194222500.648764.230940@57g2000hsv.googlegroups.com>,
> <ouyang.jie@gmail.com> wrote:
> >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

Subject: How to catch warnings using try catch

From: Phil Goddard

Date: 1 Jun, 2012 23:49:37

Message: 4 of 7

Undocumented, but has been available since at least R2008a.

Phil.

Subject: How to catch warnings using try catch

From: Kristin

Date: 2 Jun, 2012 21:39:21

Message: 5 of 7

 ouyang.jie@gmail.com wrote in message <1194222500.648764.230940@57g2000hsv.googlegroups.com>...
> Is it possible to catch warnings like "matrix is close to singular" in
> M files? Thanks.
>

Have you looked at DBSTOP? Im not sure exactly how you want to catch the errors, but I write:
>> dbstop if error
before I run an m-file, and it stops at the place where the error occurred (much like KEYBOARD).

Subject: How to catch warnings using try catch

From: Yair Altman

Date: 11 Jul, 2012 22:27:25

Message: 6 of 7

"Phil Goddard" <phil@goddardconsulting.ca> wrote in message <jqbkeh$1dq$1@newscl01ah.mathworks.com>...
> 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
 

Subject: How to catch warnings using try catch

From: William

Date: 24 Oct, 2013 16:29:07

Message: 7 of 7

 ouyang.jie@gmail.com wrote in message <1194222500.648764.230940@57g2000hsv.googlegroups.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.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us