Main Content

Restore Warnings

MATLAB® allows you to save the on-off warning states, modify warning states, and restore the original warning states. This is useful if you need to temporarily turn off some warnings and later reinstate the original settings.

The following statement saves the current state of all warnings in the structure array called orig_state:

orig_state = warning;

To restore the original state after any warning modifications, use this syntax:

warning(orig_state);

You also can save the current state and toggle warnings in a single command. For example, the statement, orig_state = warning('off','all'); is equivalent to the commands:

orig_state = warning;
warning('off','all')

Disable and Restore a Particular Warning

This example shows you how to restore the state of a particular warning.

  1. Query the Control:parameterNotSymmetric warning:

     warning('query','Control:parameterNotSymmetric')
    
    The state of warning 'Control:parameterNotSymmetric' is 'on'.

  2. Turn off the Control:parameterNotSymmetric warning:

    orig_state = warning('off','Control:parameterNotSymmetric')
    
    orig_state = 
    
        identifier: 'Control:parameterNotSymmetric'
             state: 'on'

    orig_state contains the warning state before MATLAB turns Control:parameterNotSymmetric off.

  3. Query all warning states:

    warning
    The default warning state is 'on'. Warnings not set to the default are
    
    State  Warning Identifier
    
        off  Control:parameterNotSymmetric

    MATLAB indicates that Control:parameterNotSymmetric is 'off'.

  4. Restore the original state:

    warning(orig_state)
    warning('query','Control:parameterNotSymmetric')
    The state of warning 'Control:parameterNotSymmetric' is 'on'.

Disable and Restore Multiple Warnings

This example shows you how to save and restore multiple warning states.

  1. Disable three warnings, and query all the warnings:

    w(1) = warning('off','MATLAB:rmpath:DirNotFound');
    w(2) = warning('off','MATLAB:singularMatrix');
    w(3) = warning('off','Control:parameterNotSymmetric');
    warning
    The default warning state is 'on'. Warnings not set to the default are
    
    State  Warning Identifier
    
        off  Control:parameterNotSymmetric
        off  MATLAB:rmpath:DirNotFound
        off  MATLAB:singularMatrix
  2. Restore the three warnings to their the original state, and query all warnings:

    warning(w)
    warning
    All warnings have the state 'on'.

    You do not need to store information about the previous warning states in an array, but doing so allows you to restore warnings with one command.

Note

When temporarily disabling multiple warnings, using methods related to onCleanup might be advantageous.

Alternatively, you can save and restore all warnings.

  1. Enable all warnings, and save the original warning state:

    orig_state = warning('on','all');
  2. Restore your warnings to the previous state:

    warning(orig_state)

See Also

|

Related Topics