How to clear "last warning"?

45 views (last 30 days)
Benjamin on 31 May 2012
Commented: Walter Roberson on 22 Jun 2018
SHORT QUESTION: I would like to clear the value "lastwarn" in a nice way so that I can create loops.
BACKGROUND: I want to count the number of warnings " Matrix is singular to working precision." that Matlab generates. I have read But it does not say how to clear the last warning. Currently I am creating a new warning for this purpose (see the following code). Does anybody know a better solution?
vector_input=[0 1 0]';
vector_output= -100*ones(size(vector_input));
for k=1:size(vector_input,1)
% warning on MATLAB:nearlySingularMatrix;
vector_output(k)=inv( vector_input(k));
[msglast, msgidlast] = lastwarn;
if strcmp(msgidlast,'MATLAB:singularMatrix')
s = warning('off','all'); % turn display of all warnings off
warning('FineId','fine') % becomes the last warning
warning(s) % turn display of all warnings on

Accepted Answer

Honglei Chen
Honglei Chen on 31 May 2012
Walter Roberson
Walter Roberson on 22 Jun 2018
Turns out to require lastwarn('') now.

Sign in to comment.

More Answers (0)


Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by