Using "If Warning" as a conditional statement

45 views (last 30 days)
Hi!
Short Question: I would like my code to perform in a certain way if a warning is issued, no matter the warning. Something like
dbstop if warning
however I do not want it to stop but to break the loop or do other things. Is there a function which I can use in an "if" statement? The only possibility I found was the try - catch blcok but as far as I got, that requires to know your error.
Details/Long Q.: I have a function that takes input and does matrix inversion on many matrices. Sometimes the input is bad (it is randomly generated) and the determinant is say 0, NaN, -Inf, etc. Matlab gives me a warning for that and I want it, as soon as it sees its own warning, to stop executing the funciton and return, so that it can draw again. I used conditional statements at first (if isnan(det(A)) return; end for example), but I have many matrices and the combinations are too many to do it manually. What would be a nice way to do it?

Accepted Answer

Friedrich
Friedrich on 15 Mar 2012
Hi,
you can use lastwarn to get the last warning:
[warnmsg, msgid] = lastwarn
And than you can compare the msgid from the last warning with the msgid from the warning which you get for a bad matrix.
>> inv([eps eps; eps eps])
Warning: Matrix is singular to working precision.
ans =
Inf Inf
Inf Inf
>> [warnmsg, msgid] = lastwarn
warnmsg =
Matrix is singular to working precision.
msgid =
MATLAB:singularMatrix
So you do something like this in your code:
inv(input_from_user)
[warnmsg, msgid] = lastwarn
if strcmp(msgid,'MATLAB:singularMatrix')
//warning was thrown, do something
end
  5 Comments
Boris
Boris on 15 Mar 2012
I will check this out tomorrow, thanks. It might work, (1) as long as all the bad matrices give the same warning, and (2) as long as the last warning is the first problem that occurs. To (2) - the singular matrices give me errors afterwards in 3 other functions, so it must be the first problem.
To (1) - I hope that a matrix with a det = 0 and a matrix with a det = inf or det = NaN give the same warning (singular OR CLOSE TO SINGULAR), since I can get all types of problems with my random numbers.
testplayer
testplayer on 1 Mar 2017
I think the problem with lastwarn is that it is within the base workspace and not like a local variable. For example, if you define a function using lastwarn as a conditional statement, and if you call this function within a parfor loop or a cell/array function which runs in parallel, the lastwarn can only read the warning information from the base workspace instead of the function's workspace, and the warning information in the base workspace can be modified unintentionally by other workers of parfor loop calling the same function containing the lastwarn.
As an example:
function [ x ] = smart_mldivide( A, B )
% computing the least square solution x for A*x=B by x=A\B, and when A is rank deficient, switching to x=pinv(A)*B
lastwarn('');
x = A\B;
if strncmpi(lastwarn,'Rank deficient',length('Rank deficient'))
x = pinv(A)*B;
lastwarn('');
end
end
If I call this function within a parfor loop, the state of the latest warning could be modified by other calls of this function by other workers.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!