Transform A Warning into An Error in MATLAB — Make warnings interrupt the running script like errors
When encountering errors, MATLAB will interrupt the running script and throw error message. When encountering warnings, however, MATLAB will only display warning message on the command window, but not interrupt the running script. For example, if we try to inverse a non-invertible matrix, we can see a warning, “Matrix is singular to working precision”, and MATLAB will still execute the subsequent code:
1
2
3
4
5
6
7
clc, clear, close all
a = [0, 0, 0; 1, 2, 3; 4, 5, 6];
b = inv(a)
c = b+1
1
2
3
4
5
6
7
8
9
10
11
12
Warning: Matrix is singular to working precision.
> In script1 (line 5)
b =
Inf Inf Inf
Inf Inf Inf
Inf Inf Inf
c =
Inf Inf Inf
Inf Inf Inf
Inf Inf Inf
Sometimes, the default behavior that warnings don’t interrupt the running script may disturb us when we debug the code — we may hope that warnings, like errors, will also interrupt the code running. To this end, we can utilize the warning
function1. Similarly take the above example:
1
2
3
4
5
6
7
8
9
clc, clear, close all
warning('error', 'MATLAB:singularMatrix')
a = [0, 0, 0; 1, 2, 3; 4, 5, 6];
b = inv(a)
c = b+1
then we’ll have:
1
2
3
4
5
Error using inv
Matrix is singular to working precision.
Error in script1 (line 7)
b = inv(a)
What’s more, at this time we can use try-catch
block2 to catch this error:
1
2
3
4
5
6
7
8
9
10
11
clc, clear, close all
warning('error', 'MATLAB:singularMatrix')
a = [0, 0, 0; 1, 2, 3; 4, 5, 6];
try
b = inv(a)
catch ME
ME
end
1
2
3
4
5
6
7
8
ME =
MException with properties:
identifier: 'MATLAB:singularMatrix'
message: 'Matrix is singular to working precision.'
cause: {}
stack: [1×1 struct]
Correction: []
It can be seen at this time, we can’t get the value of b
and c
, because we have transformed the warning MATLAB:singularMatrix
into an error using the warning
function (To reset it as a warning, we can use the code warning('on', 'MATLAB:singularMatrix')
.)
By the way, the warning
documentation1 doesn’t mention this point, and I found it on the Undocumented MATLAB34.
In the above example, 'MATLAB:singularMatrix'
is a warning ID and is very important in this process. So, how obtain it? There are two ways.
(1) The first one is using warning('on', 'verbose')
3:
1
2
3
4
5
6
7
clc, clear, close all
warning('on', 'verbose')
a = [0, 0, 0; 1, 2, 3; 4, 5, 6];
inv(a)
1
2
3
4
5
6
7
8
9
Warning: Matrix is singular to working precision.
(Type "warning off MATLAB:singularMatrix" to suppress this warning.)
> In script1 (line 7)
ans =
Inf Inf Inf
Inf Inf Inf
Inf Inf Inf
After getting the warning ID, we can use warning('off', 'verbose')
to reset the default setting. (“By default, the state of verbosity is set to 'off'
and the state of stack trace display is set to 'on'
.”1)
(2) The second one is using the lastwarn
function5:
1
2
3
4
5
6
7
clc, clear, close all
a = [0, 0, 0; 1, 2, 3; 4, 5, 6];
inv(a)
[msg, warnID] = lastwarn
1
2
3
4
5
6
7
8
9
10
11
12
Warning: Matrix is singular to working precision.
ans =
Inf Inf Inf
Inf Inf Inf
Inf Inf Inf
msg =
'Matrix is singular to working precision.'
warnID =
'MATLAB:singularMatrix'
where the variable warnID
is what we expect. I prefer this method.
References