Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

lastwarn

Last warning message

Syntax

msg = lastwarn
[msg,msgID] = lastwarn
[___] = lastwarn(newMsg,newID)

Description

example

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

example

[msg,msgID] = lastwarn also returns the message 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 message 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 message 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,msgID] = lastwarn;
msg =

    'Matrix is singular to working precision.'


msgID =

    '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',msgId);
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.'

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

The message identifier includes a component and mnemonic. The identifier must always contain a colon and follows this simple format: 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 whitespace characters can appear anywhere in newID. For more information, see Message Identifiers.

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

Introduced before R2006a

Was this topic helpful?