Main Content

warning

R2026b

Display and manage warning

Description

Display Warning

warning(msg) displays a warning message msg and sets the value returned by the lastwarn function. If msg is empty, warning("") resets lastwarn without displaying any text.

example

warning(msg,A1,...,An) displays a warning message that contains formatting conversion characters, such as those used with the MATLAB® sprintf function. Each conversion character in msg is converted to the corresponding scalar value in A1,...,An.

warning(warnID,___) attaches a warning identifier to the warning message. You can include any of the input arguments in the previous syntaxes. You can distinguish warnings and control their states (enable or disable) using warning identifiers.

Manage Warning

warning displays the states of all warnings.

warning(state) enables or disables all warnings, or displays their states.

example

warning(state,warnID) enables or disables a specified warning, or displays its state.

warnStruct = warning returns a structure array that contains information about the current states of warnings. Each element in warnStruct includes an identifier field with a warnID or "all", and a state field that indicates whether the warning is disabled or enabled.

example

warning(warnStruct) sets the states of warnings specified in warnStruct.

Control Display

modeStruct = warning(state,mode) controls whether MATLAB displays the stack trace or additional information by setting the specified mode to state. The optional return structure modeStruct contains information about the previous mode state.

example

warning(modeStruct) sets the mode state specified in modeStruct.

Examples

collapse all

Generate warning messages with optional format specifiers and warning identifiers.

Display a basic warning 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 using a format specifier.

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 

Enable, disable, and query warning states for all or specified warnings.

Disable all warnings.

warning("off")

Query the state of all warnings.

warning
All warnings have the state "off".

Enable all warnings and disable a specific singular matrix warning. Then, query the states of 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")

Save current warning states, modify them, and restore the saved warning states.

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

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

Save the current states of warnings.

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

Modify the warnings by disabling all of them. Then, query all warnings.

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

Restore the saved warning states, and query the states to verify.

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

State  Warning Identifier

    off  MATLAB:singularMatrix

Control whether MATLAB displays the stack trace or additional information for warnings.

Set verbose and backtrace to their default states.

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

Enable all warnings, and display a warning by removing 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 "verbose" to display additional information for the warning.

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 stack trace display.

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

Temporarily disable a specific warning, then restore its original state.

Generate a warning by computing 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 of this warning, save its warning state, and disable this warning.

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

Restore previous warning state for this warning.

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

Input Arguments

collapse all

Information about warning, specified as a character vector or string scalar. This text displays as the warning message. 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 A1,...,An input argument. The % character is treated as a format specifier. Use %% to represent a percent character in msg. For more information, see Formatting Text.

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

One or more values that replace the conversion specifiers in msg, each value is 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 a warning identifier to identify the source of a warning or to control a selected subset of 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 using warning(state,warnID), warnID can have a value of "all" or "last". Use "all" to set the states of all warnings, and use "last" to set the state of the last issued warning.

Example: "MATLAB:singularMatrix"

Example: "MATLAB:narginchk:notEnoughInputs"

State control indicator, specified as "on", "off", or "query". Use "on" or "off" to enable or disable warnings, or to enable or disable the warning display mode. Use "query" to query the current state of the warning or mode setting.

Warning states, specified as a structure array that contains information about which warnings are enabled and which are disabled. warnStruct includes an identifier field with a warnID or "all" value, and a state field with an "on" or "off" value to indicate the state of the corresponding warning.

Warning display mode, specified as "backtrace" or "verbose". Use "backtrace" to control whether to display the stack trace for warnings, and use "verbose" to control whether to display additional warning information. By default, "backtrace" is set to "on" and "verbose" is set to "off".

Output Arguments

collapse all

Warning states, specified as a structure array that contains information about which warnings are enabled and which are disabled. warnStruct includes an identifier field with a warnID or "all" value, and a state field with an "on" or "off" value to indicate the state of the corresponding warning.

Mode state, specified as a scalar structure that contains information from the previous mode state. warnStruct includes an identifier field with a "verbose" or "backtrace" value, and a state field with an "on" or "off" value to indicate the state of the corresponding mode.

Extended Capabilities

expand all

Version History

Introduced before R2006a