Controlling Warning Messages and State
There are occasional posts on the MATLAB newsgroup, such as this one, where people ask how to control when they see various warnings. One reason people might want this is to control what a user sees during some calculation. For example, you may know that a particular warning is likely to crop up during the calculation, but for the purposes of this specific calculation, it is not a worry. You'd like to turn this particular warning off in the context of your calculation, but not necessarily for the remainder of your MATLAB session.
Contents
Evolution of Warnings
In older versions of MATLAB, you couldn't control the warnings at all. We followed this with the ability to control seeing warnings or not, an all or nothing deal. So warning had a global state. More recently, we added message identifiers and the ability to control warnings more finely. We introduced these message ids was specifically so you could control the messages you see. In addition, they help us supply translated messages for the Japanese version of MATLAB on Windows.
Capture warning state for later use.
sw = warning;
Poor Programming Pattern
Let me illustrate one of the programming patterns I've seen. Here's a line of code that normally gives a warning in MATLAB R2006b:
1/0
Warning: Divide by zero. ans = Inf
Suppose I don't want to see that message. I can turn all warnings off and then enable them after I finish.
warning off 1/0 warning on
ans = Inf
The reason this is problematic is that I may have some other warnings in some non-default state. But by typing warning on, I have obliterated that information. Let me look at the state of warnings now:
swn = warning
swn = 2x1 struct array with fields: identifier state
We can see there are two elements to my warning state and they are
swn(1) swn(2)
ans = identifier: 'all' state: 'on' ans = identifier: 'MATLAB:nonScalarConditional' state: 'off'
Compare this to the original warning state I saved at the beginning of this article.
sw
sw = 8x1 struct array with fields: identifier state
The original settings were
for ind = 1:length(sw) sw(ind) end
ans = identifier: 'all' state: 'on' ans = identifier: 'MATLAB:intConvertNonIntVal' state: 'off' ans = identifier: 'MATLAB:intConvertNaN' state: 'off' ans = identifier: 'MATLAB:intMathOverflow' state: 'off' ans = identifier: 'MATLAB:intConvertOverflow' state: 'off' ans = identifier: 'MATLAB:nonScalarConditional' state: 'off' ans = identifier: 'MATLAB:mir_variable_with_script_name' state: 'error' ans = identifier: 'MATLAB:mir_warning_unrecognized_pragma' state: 'off'
Return warning state to initial one.
warning(sw)
Better Code : First Option
If you know the message id for the warning you want to suppress, simply turn that warning off. To find the id, if it exists, use lastwarn. Then set the state explicitly for that id.
1/0 [msg, msgid] = lastwarn
Warning: Divide by zero. ans = Inf msg = Divide by zero. msgid = MATLAB:divideByZero
Note that the state s captured below is the state prior to resetting the warning state. In this way, you can preserve the original state and set to a new state in one statement. Later, you can use s to restore to the original state.
s = warning('off',msgid); 1/0 warning(s) % restore state
ans = Inf
Better Code : Second Option
Not all warning messages have identifiers associated with them. If you know of a warning you don't want to see, but it does not have an associated id, you can do this.
s = warning('off','all'); % turn all warnings off if true, disp('I am running this code!') warning('Here is a warning that doesn''t have an id.') end warning(s) % restore the warning state
I am running this code!
I turned all the warnings off so you didn't see the message. So, did I actually trigger the warning? Yes, and here's how I know.
[msglast, msgidlast] = lastwarn
msglast = Here is a warning that doesn't have an id. msgidlast = ''
Summary
There are set of functions in MATLAB that allow you control the state of warnings for your work without disrupting how all functions behave. Any thoughts on when and how you use this set of features? Let me know.
Loren Shure
Copyright 2006 The MathWorks, Inc.
Published with MATLAB® 7.3
- Category:
- Best Practice,
- Common Errors,
- Robustness
Comments
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.