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:

 



Similar Reads

How to Change Warnings Display in MATLAB?
MATLAB displays warnings whenever there are errors in the execution of a code. These messages vary in detail depending on the method chosen by the user for their warning messages. MATLAB provides two modes of warning messages Verbose BacktraceIn verbose mode, MATLAB gives information on the error as well a method to suppress the warning. On the oth
3 min read
Suppress Warnings in MATLAB
MATLAB provides a facility to add Suppress Warnings. Using MATLAB's warning function, you can disable warning messages. Here are a few techniques: Suppress one warningThe warning ('off', 'identifier') command, where 'identifier' is the identifier of the warning message, can be used to suppress a particular warning message. For instance, you can use
2 min read
How to Select Random Rows from a Matrix in MATLAB?
A matrix is an n x n array that stores integers, floating point numbers or alphanumeric data in MATLAB. Indexing a matrix is the same as indexing an array. Syntax:matrix_name(i,j)where, i is the row number, and J is the column number which is to be indexed. Example 1: C/C++ Code % MATLAB code for select % random matrix elements mat = magic(5); mat(
2 min read
MATLAB Annotation
Annotations in MATLAB is a way of adding explanation or notes to the plots. Annotations add more information to the plots. There are different syntax formations for adding annotations to a plot: annotation(lineType,x,y)annotation(lineType)annotation(shapeType,dim)annotation(shapeType)annotation(___,Name,Value) Let's discuss all the above functions
3 min read
Global Variables in MATLAB
Variables in programming are generally storage spaces to store a certain type of data. There are many types of variables, but two commonly used types are local and Global variables. Generally, each MATLAB function has its own local variables. But sometimes for the sake of programming, we need to change the value of the local variable in another fun
4 min read
How to Calculate Harmonic Mean in MATLAB?
Harmonic mean is a type of mean, which is a measure of central tendencies of data, in statistics that gives large weightage to smaller data and small weightage to larger data. The Harmonic Mean in mathematical terms is nothing but the reciprocal of the mean of reciprocal values of all the data elements. Harmonic Mean in MATLAB:MATLAB provides a bui
2 min read
Copy Objects in MATLAB
In MATLAB, there are two kinds of objects - handles and values. The value objects are ordinary MATLAB objects which behave normally to copy operations. It means that when a value object is copied, the new copies of that object are completely independent of the original one i.e., if the original's value is changed, the copied object will not be chan
2 min read
How to create a function in MATLAB ?
A function is a block of statements that intend to perform a specific task. Functions allow the users to reuse the code frequently. MATLAB has several predefined functions which are ready to use such as sin(), fact(), cos() etc. MATLAB also allows the users to define their own functions. Syntax: function output_params = function_name(iput_params) %
2 min read
Creating Apps Using App Designer in MATLAB
MATLAB is a powerful, high-level programming language. Matlab is widely used for designing systems by engineers and scientists and we all know that the best way to represent any idea is by using a simple but effective GUI. Matlab app builder provides you the power to build different apps, to represent your idea in a GUI-friendly manner. In this art
3 min read
Mesh Surface Plot in MATLAB
Mesh Surface Plot is used to depict f(X, Y, Z) in a three-dimensional space. Matlab allows users to create mesh surface plots using the mesh() method. Different syntax of mesh() method are: Mesh(X, Y, Z)Mesh(Z)Mesh(___,C)Mesh(___, Name, Value)S = mesh()Mesh(ax,____)Mesh(X, Y, Z)It plots X, Y, Z on a three-dimensional surface.Matrices X, Y are plott
3 min read
Article Tags :