MATLAB Functions | Help Desk |
lasterr
Last error message
str=
lasterr lasterr('')
str = lasterr
returns the last error message generated by MATLAB.
lasterr('')
resets lasterr
so it returns an empty matrix until the next error occurs.
Here is a function that examines the lasterr
string and displays its own message based on the error that last occurred. This example deals with two cases, each of which is an error that can result from a matrix multiply.
functionThecatch l
=
lasterr; j
=
findstr(l,'Inner
matrix
dimensions'); if
j~=[]
disp('Wrong
dimensions
for
matrix
multiply') else
k
=
findstr(l,'Undefined
function
or
variable')
if
(k~=[])
disp('At
least
one
operand
does
not
exist')
end end
lasterr
function is useful in conjunction with the two-argument form of the eval
function:
eval('str','catchstr')where catchstr examines the
lasterr
string to determine the cause of the error and take appropriate action. The eval
function evaluates the string str
and returns if no error occurs. If an error occurs, eval
executes catchstr. Using eval
with the catch
function above:
clear AMATLAB responds with=
[1
2
3;
6
7
2;
0
-1
5]; B
=
[9
5
6;
0
4
9]; eval('A*B','catch')
Wrong dimensions for matrix multiply
.
error
Display error messages
eval
Interpret strings containing MATLAB expressions