Open In App

Restore Warnings in MATLAB

Last Updated : 18 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

MATLAB shows warning whenever an error or exception occurs. Generally, these warnings are insightful however, sometimes they are not needed or are needed in a modified state. For such cases, MATLAB provides the option to modify or define one’s own warnings for an error/exception.

But, there could be situations when, after altering the original state of warning settings, one needs to restore them to their original state. In this article, we shall discuss the same scenario, where we shall first modify the default warning settings and then, restore them to their original state.

Storing Original Warning Settings

MATLAB can store warning state at a given time by storing the same in a structure array. This allows to restore this sate in future. The same can be done with the following code.

Example 1:

Matlab




% Code
default_warning_state = warning;


Output:

This stores the present state of warnings in the struct type array default_warning_state.

 

Now, let us alter the state of warnings so that we can then, restore them.

Altering Warnings

First let us query the default states of some warnings, we can use the following statement for the same:

warning(‘query’, ‘<warning name>’)

This will tell whether the queried warning is set to on or off. Let us use the same on some warnings.

Example 2:

Matlab




% Code
warning('query','MATLAB:rmpath:DirNotFound')
warning('query','MATLAB:allocateMfile')


Output:

 

Now, we shall store the original state of warnings and then, change the state of above two warnings to ‘off’.

Example 3:

Matlab




% storing the original state of all warnings
default_warning_state = warning;
  
% turning off warnings
warning('off','MATLAB:rmpath:DirNotFound')
warning('off','MATLAB:allocateMfile')
  
% querying the new state
warning('query','MATLAB:rmpath:DirNotFound')
warning('query','MATLAB:allocateMfile')


Output:

Here, we first store the original state of all warnings and then, change their status to off and query the same to verify. The output shall be:

 

In the next section, we shall show to restore these two warnings to their original (‘on’) state.

Restoring Warning State

To restore the warnings to their original state, one only needs to pass the structure array storing the original state to the warning function and call it.

warning(<original_state_array>)

In our example, we use the following code to restore the warning states and then query them.

Example 4:

Matlab




% storing the original state of all warnings
default_warning_state = warning;
  
% turning off warnings
warning('off','MATLAB:rmpath:DirNotFound')
warning('off','MATLAB:allocateMfile')
  
% querying the new state
warning('query','MATLAB:rmpath:DirNotFound')
warning('query','MATLAB:allocateMfile')
  
% restoring state of warnings
warning(default_warning_state)
  
fprintf('\nAfter restoration...\n\n')
% querying the new warning state
warning('query','MATLAB:rmpath:DirNotFound')
warning('query','MATLAB:allocateMfile')


We restore the warning state by calling the warning function with the parameter default_warning_state the array storing out original warning state.

Output:

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads