Suppress Warnings
Your program might issue warnings that do not always adversely affect execution. To
avoid confusion, you can hide warning messages during execution by changing their states
from 'on'
to 'off'
.
To suppress specific warning messages, you must first find the warning identifier. Each warning message has a unique identifier. To find the identifier associated with a MATLAB® warning, reproduce the warning. For example, this code reproduces a warning thrown if MATLAB attempts to remove a nonexistent folder:
rmpath('folderthatisnotonpath')
Warning: "folderthatisnotonpath" not found in path.
Note
If this statement does not produce a warning message, use the following code to temporarily enable the display of all warnings, and then restore the original warning state:
w = warning ('on','all'); rmpath('folderthatisnotonpath') warning(w)
To obtain information about the most recently issued warning, use the
warning
or lastwarn
functions. This code
uses the query
state to return a data structure containing the
identifier and the current state of the last
warning:
w = warning('query','last')
w = identifier: 'MATLAB:rmpath:DirNotFound' state: 'on'
id
:id = w.identifier;
Note
warning('query','last')
returns the last displayed warning.
MATLAB only displays warning messages that have state:
'on'
and a warning identifier.
Using the lastwarn
function, you can retrieve the last warning
message, regardless of its display
state:
lastwarn
ans = "folderthatisnotonpath" not found in path.
Turn Warnings On and Off
After you obtain the identifier from the query
state, use this
information to disable or enable the warning associated with that identifier.
Continuing the example from the previous section, turn the warning
'MATLAB:rmpath:DirNotFound'
off, and repeat the
operation.
warning('off',id) rmpath('folderthatisnotonpath')
MATLAB displays no warning.
Turn the warning on, and try to remove a nonexistent path:
warning('on',id) rmpath('folderthatisnotonpath')
Warning: "folderthatisnotonpath" not found in path.
MATLAB now issues a warning.
Tip
Turn off the most recently invoked warning with
warning('off','last')
.
Controlling All Warnings
The term all refers only to those warnings that have been issued or modified during your current MATLAB session. Modified warning states persist only through the current session. Starting a new session restores the default settings.
Use the identifier 'all'
to represent the group of all
warnings. View the state of all warnings with either
syntax:
warning('query','all')
warning
To enable all warnings and verify the state:
warning('on','all') warning('query','all')
All warnings have the state 'on'.
To disable all warnings and verify the state, use this syntax:
warning('off','all') warning
All warnings have the state 'off'.