MATLAB Function Reference Previous page   Next Page
warning

Display warning message

Syntax

Description

warning('message') displays the text 'message' like the disp function, except that with warning, message display can be suppressed.

warning('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.

warning('message_id', 'message') attaches a unique identifier, or message_id, to the warning message. The identifier enables you to single out certain warnings during the execution of your program, controlling what happens when the warnings are encountered. See Message Identifiers and Warning Control in the MATLAB documentation for more information on the message_id argument and how to use it.

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

s = warning(state, 'message_id') is a warning control statement that enables you to indicate how you want MATLAB to act on certain warnings. The state argument can be 'on', 'off', or 'query'. The message_id argument can be a message identifier string, 'all', or 'last'. See Control Statements in the MATLAB documentation for more information.

Output s is a structure array that indicates the previous state of the selected warnings. The structure has the fields identifier and state. See Output from Control Statements in the MATLAB documentation for more.

s = warning(state, mode) is a warning control statement that enables you to enter debug mode, display an M-stack trace, or display more information with each warning. The state argument can be 'on', 'off', or 'query'. The mode argument can be 'debug', 'backtrace', or 'verbose'. See Debug, Backtrace, and Verbose in the MATLAB documentation for more information.

Examples

Example 1

Generate a warning that displays a simple string:

Example 2

Generate a warning string that is defined at run-time. The first argument defines a message identifier for this warning:

Example 3

Using a message identifier, enable just the actionNotTaken warning from Simulink by first turning off all warnings and then setting just that warning to on:

Use query to determine the current state of all warnings. It reports that you have set all warnings to off with the exception of Simulink:actionNotTaken:

Example 4

MATLAB converts special characters (like \n and %d) in the warning message string only when you specify more than one input argument with warning. 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 is true regardless of whether the additional argument supplies conversion values or is a message identifier:

Example 5

To enter debug mode whenever a parameterNotSymmetric warning is invoked in a component called Control, first turn off all warnings and enable only this one type of warning using its message identifier. Then turn on debug mode for all enabled warnings. When you run your program, MATLAB will stop in debug mode just before this warning is executed. You will see the debug prompt (K>>) displayed:

Example 6

Turn on one particular warning, saving the previous state of this one warning in s. Remember that this nonquery syntax performs an implicit query prior to setting the new state:

After doing some work that includes making changes to the state of some warnings, restore the original state of all warnings:

See Also

lastwarn, warndlg, error, lasterr, errordlg, dbstop, disp, sprintf


Previous page  warndlg waterfall Next page

© 1994-2005 The MathWorks, Inc.