| MATLAB Function Reference | ![]() |
Syntax
str=lasterr lasterr('')
Description
str = lasterr
returns the last error message generated by MATLAB.
lasterr('')
resets lasterr so it returns an empty matrix until the next error occurs.
Examples
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.
functioncatchfcn l=lasterr; j=findstr(l,'Innermatrixdimensions'); if~isempty(j)disp('Wrongdimensionsformatrixmultiply') elsek=findstr(l,'Undefinedfunctionorvariable');if~isempty(k)disp('Atleastoneoperanddoesnotexist')end end
The lasterr function is useful in conjunction with the two-argument form of the eval function:
eval('string','catchstr')
or the try ... catch...end statements. The catch action examines the lasterr string to determine the cause of the error and takes appropriate action.
The eval function evaluates string and returns if no error occurs. If an error occurs, eval executes catchstr. Using eval with the catchfcn function above:
clear A=[123;672;0-15]; B=[956;049]; eval('A*B','catchfcn')
MATLAB responds with Wrong dimensions for matrix multiply.
See Also
| kron | lastwarn | ![]() |