Main Content

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:

Warning: "folderthatisnotonpath" not found in path. 


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');

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'
You can save the identifier field in the variable, id:
id = w.identifier;


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:

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.


MATLAB displays no warning.

Turn the warning on, and try to remove a nonexistent path:

Warning: "folderthatisnotonpath" not found in path.

MATLAB now issues a warning.


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:


To enable all warnings and verify the state:

All warnings have the state 'on'.

To disable all warnings and verify the state, use this syntax:

All warnings have the state 'off'.

Related Topics