Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB   

warning - Warning message

Syntax

warning('message')
warning('message', a1, a2,...)
warning('message_id', 'message')
warning('message_id', 'message', a1, a2, ..., an)
s = warning(state, 'message_id')
s = warning(state, mode)

Description

warning('message') displays descriptive text message and sets the warning state that lastwarn returns. If message is an empty string (''), warning resets the warning state but does not display any text.

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 Programming Fundamentals 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 Warning Control Statements in the MATLAB Programming Fundamentals 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 Programming Fundamentals documentation for more.

s = warning(state, mode) is a warning control statement that enables you to 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 'backtrace' or 'verbose'. See Backtrace and Verbose Modes in the MATLAB Programming Fundamentals documentation for more information.

Examples

Example 1

Generate a warning that displays a simple string:

if ~ischar(p1)
   warning('Input must be a string')
end

Example 2

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

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

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:

warning off all
warning on Simulink:actionNotTaken

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:

warning query all
The default warning state is 'off'. Warnings not set to the default are

  State  Warning Identifier

     on  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:

warning('In this case, the newline \n is not converted.')
Warning: In this case, the newline \n is not converted.

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:

warning('WarnTests:convertTest', ...
      'In this case, the newline \n is converted.')
Warning: In this case, the newline 
 is converted.

Example 5

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:

s = warning('on', 'Control:parameterNotSymmetric');

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

warning(s)

See Also

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

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

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