Main Content

warning

Display warning message

Description

example

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

warning(msg,A) displays a message that contains formatting conversion characters, such as those used with the MATLAB® sprintf function. Each conversion character in msg is converted to one of the values A.

warning(warnID,___) 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,warnID) 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 warnID 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

collapse all

Generate a warning that displays a message.

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

Include information about n in the warning message.

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

Attach a warning identifier to the warning message.

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

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

Re-enable the singular matrix warning.

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

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

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

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 identifier, save the current warning state, and disable the specific warning.

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

Restore previous warning state.

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

Input Arguments

collapse all

Information about the cause of the warning and how you might correct it, specified as a character vector or string scalar. To format the message, 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 A input argument. For more information, see Formatting Text.

Note

You must specify more than one input argument with warning if you want MATLAB to convert special characters (such as \t, \n, %s, and %d) in the warning message.

Example: 'Input must be a character vector.'

Value that replace the conversion specifiers in msg, specified as a character vector, string scalar, or numeric scalar.

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

The warning identifier includes one or more component fields and a mnemonic field. Fields must be separated with colon. For example, a warning identifier with a component field component and a mnemonic field mnemonic is specified as 'component:mnemonic'. The component and mnemonic 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 warnID. For information on creating identifiers, see MException.

When you set the state of a warning, warnID 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'

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.

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 warnID or 'all', and state field indicating the state of the corresponding warning.

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

Extended Capabilities

Version History

Introduced before R2006a