Last error message and related information
s = lasterror
s = lasterror(err)
s = lasterror('reset')
s = lasterror returns a structure
s containing information about the most recent error issued by
MATLAB®. The returned structure contains the following fields:
Character array containing the text of the error message.
Character array containing the identifier for the error. If the
last error issued by MATLAB had no error identifier, then the
Structure providing information on the location of the error.
The structure has fields
The returned structure by
lasterror might contain additional
fields in future versions of MATLAB.
The fields of the structure returned in
Name of the file in which the function generating the error appears. This field is the empty character vector if there is no file.
Name of the function in which the error occurred. If this is
the primary function in the file, and the function name differs from
the file name,
Line number of the file at which the error occurred.
For more information about error identifiers, see
s = lasterror(err) sets the last error
information to the error message and identifier specified in the structure
err. Subsequent invocations of
return this new error information. The optional return structure
contains information about the previous error.
s = lasterror('reset') sets the last error
information to the default state. In this state, the
identifier fields of the returned structure are empty character
vectors, and the
stack field is a 0-by-1 structure.
Save the following MATLAB code in a file called
function y = average(x) % AVERAGE Mean of vector elements. % AVERAGE(X), where X is a vector, is the mean of vector elements. % Nonvector input results in an error. check_inputs(x) y = sum(x)/length(x); % The actual computation function check_inputs(x) [m,n] = size(x); if (~((m == 1) || (n == 1)) || (m == 1 && n == 1)) error('AVG:NotAVector', 'Input must be a vector.') end
Now run the function. Because this function requires vector input, passing a
scalar value to it forces an error. The error occurs in subroutine
average(200) Error using average>check_inputs (line 11) Input must be a vector. Error in average (line 5) check_inputs(x)
Get the three fields from
err = lasterror err = message: [1x61 char] identifier: 'AVG:NotAVector' stack: [2x1 struct]
Display the text of the error message:
msg = err.message msg = Error using average>check_inputs (line 11) Input must be a vector.
Display the fields containing the
err.stack is a 2-by-1 structure because it provides
information on the failing subroutine
check_inputs and also the
outer, primary function
st1 = err.stack(1,1) st1 = file: 'd:\matlab_test\average.m' name: 'check_inputs' line: 11 st2 = err.stack(2,1) st2 = file: 'd:\matlab_test\average.m' name: 'average' line: 5
As a rule, the name of your primary function should be the same as the name of
the file that contains that function. If these names differ, MATLAB uses the file name in the
name field of the
try do_something catch do_cleanup rethrow(lasterror) end
lasterroris not recommended
Not recommended starting in R2007b
MathWorks® is gradually transitioning MATLAB error handling to an object-oriented scheme that is based on the
MException class. Although support for
lasterror is expected to continue, using the static
MException.last method of
not guaranteed to always return identical results. For example,
MException.last updates its error status only on
uncaught errors, where
lasterror can update its error
status on any error, whether it is caught or not.