Main Content

lastwarn

Last warning message

Description

example

msg = lastwarn returns the last warning message generated by MATLAB®, regardless of the display state of the warning.

example

[msg,warnID] = lastwarn also returns the warning identifier associated with msg.

[___] = lastwarn(newMsg,newID) sets the last warning message and the last warning identifier. Subsequent calls to the lastwarn function return the new warning message and, if requested, the new warning identifier. You can use this syntax with any of the output arguments of the previous syntaxes.

Examples

collapse all

Display a warning message.

warning('Message 1.')
Warning: Message 1.

Call the lastwarn function to display the last warning message.

msg = lastwarn
msg =

    'Message 1.'

Save the current warning settings, and then disable all warnings.

origState = warning;
warning('off')

Call the warning function with a different message. The warning is not displayed.

warning('Message 2.')

Call the lastwarn function. MATLAB returns the last warning message, even though it was not displayed.

msg = lastwarn
msg =

    'Message 2.'

Restore the saved warning state.

warning(origState)

The warning identifier is often used to suppress warnings. To find the identifier, use the lastwarn function.

Generate a singular matrix warning.

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

Find the identifier of the warning.

[msg,warnID] = lastwarn;
msg =

    'Matrix is singular to working precision.'


warnID =

    'MATLAB:singularMatrix'

Save the current warning state, and disable the specific warning. Perform the original calculation again. This time the warning does not display.

warnStruct = warning('off',warnID);
C = B\A;

Restore the previous warning state.

warning(warnStruct);

Input Arguments

collapse all

New message for last warning, specified as a character vector or string scalar. Subsequent calls to the lastwarn function return the new warning message.

You can set the new message to an empty character vector ('') or string scalar ("").

Example: 'Warning message to display.'

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

A 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 the warning identifier. For more information on creating identifiers, see MException.

Example: 'MATLAB:singularMatrix'

Example: 'MyProject:myFunction:notEnoughInputs'

Output Arguments

collapse all

Last warning message generated by MATLAB, regardless of the display state of the warning, returned as a character vector.

Last warning identifier, returned as a character vector. If the warning was not defined with an identifier, lastwarn returns an empty character vector for warnID.

Introduced before R2006a