lastwarn - Last warning message - MATLAB (original) (raw)
Main Content
Syntax
Description
[msg](#d126e981125) = lastwarn
returns the last warning message generated by MATLAB®, regardless of the display state of the warning.
[[msg](#d126e981125),[warnID](#d126e981146)] = lastwarn
also returns the warning identifier associated with msg
.
[___] = lastwarn([newMsg](#d126e981047),[newID](#d126e981079))
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
Display a warning message.
Call the lastwarn
function to display the last warning message.
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.
Call the lastwarn
function. MATLAB returns the last warning message, even though it was not displayed.
Restore the saved warning state.
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 =
'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.
Input Arguments
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 fieldcomponent
and a mnemonic fieldmnemonic
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
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
.
Extended Capabilities
Version History
Introduced before R2006a