Programming Previous page   Next Page


Like error, the warning function alerts the user of unexpected conditions detected when running a program. However, warning does not halt the execution of the program. It displays the specified warning message and then continues.

Reporting a Warning

Use warning in your code to generate a warning message during execution. Specify the message string as the input argument to warning. For example,

Warnings also differ from errors in that you can disable any warnings that you don't want to see. You do this by invoking warning with certain control parameters. See Warning Control for more information.

Formatted Message Strings

The warning message string that you specify can also contain formatting conversion characters, such as those used with the MATLAB sprintf function. Make the warning string the first argument, and then add any variables used by the conversion as subsequent arguments.

For example, if your program cannot process a given parameter, you might report a warning with

MATLAB converts special characters (like \n and %d) in the warning message string only when you specify more than one input argument with warning. See Formatted String Conversion for information.

Message Identifiers

Use a message identifier argument with warning to attach a unique tag to that warning message. MATLAB uses this tag to better identify the source of a warning. The first argument in this example is the message identifier.

See Warning Control Statements for more information on how to use identifiers with warnings.

Identifying the Cause

The lastwarn function returns a string containing the last warning message issued by MATLAB. You can use this to enable your program to identify the cause of a warning that has just been issued. To return the most recent warning message to the variable warnmsg, type

You can also change the text of the last warning message with a new message or with an empty string as shown here.

Previous page  Using Message Identifiers with lasterr Warning Control Next page

© 1994-2005 The MathWorks, Inc.