How to Change Warnings Display in MATLAB?
Last Updated :
17 Mar, 2023
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
- Backtrace
In verbose mode, MATLAB gives information on the error as well a method to suppress the warning. On the other hand, in backtrace mode, MATLAB gives the location inside a code execution where, the warning generated.
In this article, we shall see how to use warning displays using the above modes.
Warning – Verbose mode
To turn on the verbose mode of warnings, one has to include a simple statement in their code.
warning on verbose
—OR—
warning(‘on’, ‘verbose’)
Both statements work the same thing and can be used interchangeably, as they will both turn on the verbose mode for warnings, which in turn, displays an extra message about how to suppress the warning.
Example 1
Here we shall turn on the verbose mode for a system-generated warning.
Matlab
warning on verbose
rmpath( 'abc' )
|
In this code, we generate an error by removing a dummy path (that does not exist) using the rmpath function from the MATLAB path. Then we shall see the verbose mode of this warning by turning the same on.
As we can see in the output, the warning is followed by the method to suppress it.
Example 2
We shall perform the same task as the previous example but, we will use a different method to change the warning mode.
Matlab
warning( 'on' , 'verbose' )
rmpath( 'abc' )
|
Again, we remove a path that does not exists, to generate an error. However, this time we use warning() as a function call and then pass the parameters as state of mode and mode type. The output shall remain same.
Warning – backtrace mode
In the backtrace warning mode, MATLAB provides the necessary information on the error along with the location of error in the code. This mode is useful when working with large codes where it is hard to find the error manually. The location displayed by MATLAB is a hyperlink that will redirect you to the error statement.
Backtrace mode can also be turned in two manners, similar to verbose mode.
warning on backtrace
—OR—
warning(‘on’, ‘backtrace’)
Let us see an example of each variant.
Example 3
We shall use the same example of removing the not existing path to generate an error.
Matlab
warning on backtrace
rmpath( 'backtrace' )
|
The output is:
As can be seen, MATLAB gives the location of the error-generating statement with a hyperlink.
Example 4
Now, we shall use the other variant to generate the same results.
Matlab
warning( 'on' , 'backtrace' )
rmpath( 'backtrace' )
|
Here, we use warning as a function call to turn on backtrace mode. We shall receive the same output as before.
Share your thoughts in the comments
Please Login to comment...