Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab: How to catch warning

I am running some data processing work in MATLAB and solver uses BACKSLASH operator. Sometimes, I get warning like this:

Warning: Rank deficient, rank = 1390, tol = 1.335195e-010.
Warning: Rank deficient, rank = 1386, tol = 1.333217e-010.

I would like to catch those warnings. I am trying to convert warning to error and then catch it as described here under title “Trapping warnings”: http://undocumentedmatlab.com/blog/trapping-warnings-efficiently In the example, following string has been used to convert warning to error:

s = warning('error', 'MATLAB:DELETE:Permission');

However, I am not sure what string to use for my case. I tried using

s = warning('error', 'Warning: Rank deficient’);

But, it did not work. Any help would be appreciated.

Regards, DK

like image 333
Garima Singh Avatar asked Jun 23 '14 09:06

Garima Singh


People also ask

How do you catch warnings in MATLAB?

Using the undocumented syntax warning('error', 'mycomponent:myMessageID') will tell MATLAB to convert the warning to an error, which you can then catch with a try-catch block and handle appropriately: You can find the message ID for your warning using lastwarn just after it occurs.

How do I stop a warning in MATLAB?

MATLAB now issues a warning. Turn off the most recently invoked warning with warning('off','last') .

What is warning off in MATLAB?

You can selectively enable or disable specific warning messages, as long as they have been given a message identifier, with the WARNING function using the state and 'message_id' input arguments. You can suppress all warning messages using the 'off' state option, with the argument 'all' in place of a message identifier.

How do you catch warnings in Javascript?

filter(e => typeof e == 'string'); for(m in messages){ if(messages[m]. indexOf('The warning I am looking for...') != -1){ //treat the warning as you want //you could use switch case if you want }; }; return console.


1 Answers

You need to specify the warning identifier, not the warning text. You can find the identifier using the two-output form of lastwarn:

[msgstr, msgid] = lastwarn

In your case, I think the identifier you want is 'MATLAB:rankDeficientMatrix'.

like image 111
Edric Avatar answered Nov 01 '22 07:11

Edric