Identifying the Cause
Once an error has been caught, you will need to know the source of the error in order to handle it appropriately. The
lasterr function returns information that enables you to identify the error that was most recently generated by MATLAB.
To return the most recent error message to the variable
You can also change the text of the last error message with a new message or with an empty string as shown below. You might want to do this if a lower level routine detects an error that you don't want visible to the upper levels.
Example Using lasterr
matrixMultiply function shown earlier in this section could fail for various reasons. If it is called with incompatible matrices, for example,
lasterr returns the following string.
This example uses
lasterr to determine the cause of an error in
When calling the function with two matrices not compatible for matrix multiplication, you get the following error message.
When calling the function with a cell array argument, you get a message that addresses that error.
|Handling and Recovering from an Error||Regenerating an Error|
© 1994-2005 The MathWorks, Inc.