Documentation Center

  • Trials
  • Product Updates

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.

    Note   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 Example 3 below.

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.

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

Output s is a structure array that indicates the previous state of the selected warnings. The structure has the fields identifier and state.

s = warning(state, mode) is a warning control statement that enables you to display a 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'.

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

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 4

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

| | | | | | |

Related Examples

Was this topic helpful?