Using "If Warning" as a conditional statement
45 views (last 30 days)
Show older comments
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?
1 Comment
Pankaj
on 30 Jan 2016
For the accepted answer kindly see the link to clear LASTWARN http://www.mathworks.com/matlabcentral/answers/39993-how-to-clear-last-warning
Accepted Answer
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
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.
More Answers (0)
See Also
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!