Programming Previous page   Next Page

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 errormsg, type

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

The 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 matrixMultiply.

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.


Previous page  Handling and Recovering from an Error Regenerating an Error Next page

© 1994-2005 The MathWorks, Inc.