lasterr
Last error message
lasterr is not recommended. Use MException instead. For more information, see Version History.
Syntax
msgstr
= lasterr
[msgstr,errID] = lasterr
lasterr('new_msgstr')
lasterr('new_msgstr','new_errID')
[msgstr,errID] = lasterr('new_msgstr','new_errID')
Description
msgstr
= lasterr returns the last
error message generated by MATLAB®.
[msgstr,errID] = lasterr returns the last
error in msgstr and its identifier in errID. If the
error was not defined with an identifier, lasterr returns an empty
character vector for errID. For more information about error
identifiers, see MException.
lasterr('new_msgstr') sets the last error
message to a new character vector, new_msgstr, so that subsequent
invocations of lasterr return the new error message. You can also set
the last error to an empty character vector with lasterr('').
lasterr('new_msgstr','new_errID') sets the
last error message and error identifier to new_msgstr and
new_errID, respectively. Subsequent invocations of
lasterr return the new error message and error identifier.
[msgstr,errID] = lasterr('new_msgstr','new_errID')
returns the last error message and error identifier, also changing these values so that
subsequent invocations of lasterr return the message and identifier
specified by new_msgstr and new_errID
respectively.
Examples
Example 1
Here is a function that examines the lasterr character vector
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
multiplication:
function matrix_multiply(A,B)
try
A * B
catch
errmsg = lasterr;
if(strfind(errmsg, 'Inner matrix dimensions'))
disp('** Wrong dimensions for matrix multiply')
else
if(strfind(errmsg, 'not defined for variables of class'))
disp('** Both arguments must be double matrices')
end
end
endIf you call this function with matrices that are incompatible for matrix
multiplication (e.g., the column dimension of A is not equal to the
row dimension of B), MATLAB catches the error and uses lasterr to determine its
source:
A = [1 2 3; 6 7 2; 0 -1 5]; B = [9 5 6; 0 4 9]; matrix_multiply(A,B) ** Wrong dimensions for matrix multiply
Example 2
Specify an error identifier and error message with error:
error('MyToolbox:angleTooLarge',...
'The angle specified must be less than 90 degrees.');In your error handling code, use lasterr to determine the error
identifier and error message for the failing operation:
[errmsg,errID] = lasterr errmsg = The angle specified must be less than 90 degrees. errID = MyToolbox:angleTooLarge