warning

Display warning message

Syntax

  • warning(message) example
  • warning(message,A1,...,An)
  • warning(messageID,___)
  • warning(state) example
  • warning(state,messageID)
  • warning
  • warnStruct = warning example
  • warning(warnStruct)
  • warning(state,mode) example
  • warnStruct = warning(state,mode)

Description

example

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

warning(message,A1,...,An) 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,...,An.

warning(messageID,___) attaches a warning identifier to the warning message. You can include any of the input arguments in the previous syntaxes. The identifier enables you to distinguish warnings and to control what happens when MATLAB encounters the warnings.

example

warning(state) enables, disables or displays the state of all warnings.

warning(state,messageID) acts on the state of a specified warning.

warning displays the state of all of the warnings. It is equivalent to warning('query').

example

warnStruct = warning returns a structure or array of structures that contains information about which warnings are enabled and disabled. warnStruct includes an identifier field with a messageID or 'all', and a state field that indicates the state of the corresponding warning.

warning(warnStruct) sets the current warning settings as indicated in the structure array, warnStruct.

example

warning(state,mode) controls whether MATLAB displays the stack trace or additional information about the warning.

warnStruct = warning(state,mode) returns a structure with an identifier field containing the mode and a state field containing the current state of mode. If you pass the output structure, warnStruct, into the warning function, you set the state of the mode, not which warnings are enabled or disabled.

Examples

expand all

Issue Warning Message

Generate a warning that displays a message.

n = 7;
if ~ischar(n)
   warning('Input must be a string')
end
Warning: Input must be a string

Include information about n in the warning message.

if ~ischar(n)
   warning('Input must be a string, not a %s',class(n))
end
Warning: Input must be a string, not a double

Attach a message identifier to the warning message.

if ~ischar(n)
   warning('MyComponent:incorrectType',...
       'Input must be a string, not a %s',class(n))
end
Warning: Input must be a string, not a double 

Set and Query Warning State

Disable all warnings.

warning('off')

Query the warnings.

warning
All warnings have the state 'off'.

Enable all warnings, disable the singular matrix warning, and query all warnings.

warning('on')
warning('off','MATLAB:singularMatrix')
warning
The default warning state is 'on'. Warnings not set to the default are

State  Warning Identifier

    off  MATLAB:singularMatrix

Reenable the singular matrix warning.

warning('on','MATLAB:singularMatrix')

Save and Restore Warning Settings

Enable all warnings, and then disable the singular matrix warning.

warning('on')
warning('off','MATLAB:singularMatrix')

Save the current warning settings.

s = warning
s = 

2x1 struct array with fields:

    identifier
    state

Examine the two structures.

s(1)
ans = 

    identifier: 'all'
         state: 'on'
s(2)
ans = 

    identifier: 'MATLAB:singularMatrix'
         state: 'off'

All warnings are enabled except for 'MATLAB:singularMatrix'.

Disable and query all warnings.

warning('off')
warning('query')
All warnings have the state 'off'.

Restore the saved warning state structure, and query the state.

warning(s)
warning('query')
The default warning state is 'on'. Warnings not set to the default are

State  Warning Identifier

    off  MATLAB:singularMatrix

Control Warning Verbosity

Ensure verbose and backtrace settings are the default values.

warning('off','verbose')
warning('on','backtrace')

Turn on all warnings, and remove a folder that does not exist on the MATLAB path.

warning('on')
rmpath('nosuchfolder')
Warning: "nosuchfolder" not found in path. 
> In rmpath at 57 

Enable verbosity to display an extended warning message.

warning('on','verbose')
rmpath('nosuchfolder')
Warning: "nosuchfolder" not found in path.
(Type "warning off MATLAB:rmpath:DirNotFound" to suppress this warning.)
 
> In rmpath at 57 

Disable display of the stack trace.

warning('off','backtrace')
rmpath('nosuchfolder')
Warning: "nosuchfolder" not found in path.
(Type "warning off MATLAB:rmpath:DirNotFound" to suppress this warning.)

Temporarily Disable Warning

Compute a singular matrix.

A = eye(2);
B = [3 6; 4 8];
C = B\A;
Warning: Matrix is singular to working precision.

Find the warning ID, save the current warning state, and disable the specific warning

[msgStr,msgId] = lastwarn;
warnStruct = warning('off',msgId);
C = B\A;

Restore previous warning state.

warning(warnStruct);
C = B\A;
Warning: Matrix is singular to working precision.

Input Arguments

expand all

message — Information about cause of warningstring

Information about the cause of the warning and how you might correct it, specified as a string. To format the string, use escape sequences, such as \t or \n. You also can use any format specifiers supported by the sprintf function, such as %s or %d. Specify values for the conversion specifiers via the A1,...,An input arguments. For more information, see Formatting Strings.

    Note:   MATLAB converts special characters (such as \n and %d) in the warning message string only when you specify more than one input argument with warning.

Example: 'Input must be a string.'

A1,...,An — Numeric or character arraysscalar | vector | matrix | multidimensional array

Numeric or character arrays, specified as a scalar, vector, matrix, or multidimensional array. This input argument provides the values that correspond to and replace the conversion specifiers in message.

messageID — Identifier for warningstring | 'all' | 'last'

Identifier for the warning, specified as a string, 'all', or 'last'. Use the warning identifier to better identify the source of the warning or to control a selected subset of the warnings in your program.

The message identifier includes a component and mneumonic. The identifier must always contain a colon and follows a simple format: component:mnemonic. The component and mneumonic fields must each begin with a letter. The remaining characters can be alphanumerics (A–Z, a–z, 0–9) and underscores. No white space characters can appear anywhere in messageID. For more information, see Message Identifiers.

When you set the state of a warning, the messageID can have a value of 'all' or 'last'. Use 'all' to set the state of all warnings, and use 'last' to set the state of the last issued warning.

Example: 'MATLAB:singularMatrix'

Example: 'MATLAB:narginchk:notEnoughInputs'

state — Warning control indicator'on' | 'off' | 'query'

Warning control indicator specified as 'on', 'off', or 'query'. Use 'on' or 'off' to control whether MATLAB issues a warning. Use 'query' to query the current state of the warning.

warnStruct — Warning settingsstructure | array of structures

Warning settings, specified as a structure or array of structures that contains information about which warnings are enabled and which are disabled. warnStruct includes an identifier field with a messageID or 'all', and state field indicating the state of the corresponding warning.

mode — Verbosity and sack trace display settings'backtrace' | 'verbose'

Verbosity and the stack trace display of settings, specified by 'backtrace' or 'verbose'. By default, the state of verbosity is set to 'off' and the state of stack trace display is set to 'on'.

Was this topic helpful?