Suppress Warnings in MATLAB
Last Updated :
02 Apr, 2023
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 warning
The 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: to disable the singular matrix warning message.
warning(‘off’, ‘MATLAB:singularMatrix’)
Suppress all warnings
You can use the caution (‘off’, ‘all’) command to suppress all alert messages. For instance:
warning(‘off’, ‘all’)
With this, all MATLAB alerts will be disabled.
Reset the preset warnings
The warning (‘on’, ‘all’) command can be used to return the warning status to its default state. For instance:
warning(‘on’, ‘all’)
By doing this, you can return the warning status to its initial state, where all warnings are shown.
Suppressing warnings can be advantageous, but it can also hide potential issues in your code, it’s essential to remember this. Use it wisely, and after you’re done debugging your code, make sure to switch the warnings back on.
Example 1:
Matlab
clc;
X = [4 5; 6 0];
warning( 'off' , 'all' )
warning
Y = 1 ./ X;
warning( 'on' , 'MATLAB:divideByZero' )
disp(Y)
|
Output:
This code generates a 2×2 matrix X with a divide-by-zero warning, then uses the warning function’s ‘off’ option to suppress the warning before carrying out the division operation. The outcome is displayed using the disp function, and the warning is turned back on using the warning function with the ‘on’ option.
Suppressing warnings can be helpful for clearing up the MATLAB terminal output’s disorder.
Share your thoughts in the comments
Please Login to comment...