MATLAB Function Reference    

Display error messages



error('message') displays an error message and returns control to the keyboard. The error message contains the input string message.

The error command has no effect if message is a null string.

error('message',a1,a2,...) displays a message string that contains formatting conversion characters, such as those used with the MATLAB sprintf function. Each conversion character in message is converted to one of the values a1, a2, ... in the argument list.

error('message_id','message') attaches a unique message identifier, or message_id, to the error message. The identifier enables you to better identify the source of an error. See Message Identifiers and Using Message Identifiers with lasterr in the MATLAB documentation for more information on the message_id argument and how to use it.

error('message_id','message',a1,a2, ...) includes formatting conversion characters in message, and the character translations a1, a2, ...


Example 1

The error function provides an error return from M-files:

The returned error message looks like this:

Example 2

Specify a message identifier and error message string with error:

In your error handling code, use lasterr to determine the message identifier and error message string for the failing operation:

Example 3

MATLAB converts special characters (like \n and %d) in the error message string only when you specify more than one input argument with error. In the single argument case shown below, \n is taken to mean backslash-n. It is not converted to a newline character:

But, when more than one argument is specified, MATLAB does convert special characters. This holds true regardless of whether the additional argument supplies conversion values or is a message identifier:

See Also

lasterr, lasterror, rethrow, errordlg, warning, lastwarn, warndlg, dbstop, disp, sprintf

  erf, erfc, erfcx, erfinv, erfcinv errorbar