Warnings

Reporting a Warning

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.

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

warning('Input must be a string')

Warnings also differ from errors in that you can disable any warnings that you do not 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 you specify can contain formatting conversion characters, such as those used with the MATLAB® sprintf function. Make the warning string the first argument, and add any variables used by the conversion as subsequent arguments.

warning('formatted_warningmsg', arg1, arg2, ...)

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

warning('Ambiguous parameter name, "%s".', param)

MATLAB converts special characters like %d and %s in the warning message string only when you specify more than one input argument with warning. See Formatted Message Strings for information.

Message Identifiers

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

warning('MATLAB:paramAmbiguous', ... 
        'Ambiguous parameter name, "%s".', param)

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

warnmsg = lastwarn;

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

lastwarn('newwarnmsg');   % Replace last warning with new string
lastwarn('');           % Replace last warning with empty string
  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS