Programming Previous page   Next Page

Using Message Identifiers with lasterr

One use of message identifiers is to enable the lasterr and lasterror functions to better identify the source of an error. These functions return a message identifier, and you can use the combination of component and mnemonic parts of that identifier to identify both a broad category of errors and a specific instance within that category, respectively.

The first step in using this feature is to determine which of your error messages need this type of identification and then tag each with an identifier. You do this by specifying a message identifier argument (msg_id, below) along with the error message in your calls to error. Either form shown below is acceptable. The latter form uses formatting conversion characters as described in Formatted Message Strings.

The message identifier must be the first argument and must be formatted according to the rules covered in Message Identifiers.

The message identifier is not a required argument for error. If you don't need to return this type of information with lasterr, then you can omit the msg_id argument from the error function syntax shown previously:

Returning a Message Identifier from lasterr

Use lasterr with one output to return just the string holding the error message from the most recently generated error.

Use lasterr with two outputs to return both error message string and the message identifier for that error.

The following example performs an operation in the try segment of the try-catch block that results in an error. The first line of the catch segment retrieves both the error message string and message identifier for the error. The example then responds to the error in a manner that depends on the identifier returned.

If the last error has no message identifier tag associated with it, then MATLAB returns an empty string in the second output argument.

Inputs to lasterr

In addition to returning information about the last error, lasterr also accepts inputs that modify the MATLAB copy of the last error. Use the command format shown below to change the error message string and message identifier returned by subsequent invocations of lasterr.

All lasterr input arguments are optional, but if you specify both an error message and message identifier input, they must appear in the order shown above.

Previous page  Message Identifiers Warnings Next page

© 1994-2005 The MathWorks, Inc.