Explanation

Using lasterror and lasterr for obtaining information about an error (such as the message ID and the stack trace) is not recommended because errors in other activities (such as callbacks) can result in these functions returning unexpected values.

If you specify a variable with a catch block and an error occurs in the try block, MATLAB enters the catch block and assigns the MException object to the catch variable. The MException object contains the error message, the message ID, and the stack trace. To pass this information to a calling function, either return the MException object as an output of your function, or call its rethrow method to rethrow the error. There is no need to save and restore lasterr around such a try/catch block.


Suggested Action

Specify a variable with a catch block, as described in ”The try-catch Statement”.