Documentation

lastwarn

Last warning message

Syntax

msgstr = lastwarn
[msgstr, msgid] = lastwarn
lastwarn('new_msgstr')
lastwarn('new_msgstr', 'new_msgid')
[msgstr, msgid] = lastwarn('new_msgstr', 'new_msgid')

Description

msgstr = lastwarnreturns the last warning message generated by MATLAB®, regardless of its display state.

[msgstr, msgid] = lastwarn返回最后一个警告msgstrand its message identifier inmsgid. If the warning was not defined with an identifier,lastwarnreturns an empty character vector formsgid.

lastwarn('new_msgstr')sets the last warning message to a new character vector,new_msgstr, so that subsequent invocations oflastwarnreturn the new warning message. You can also set the last warning to an empty character vector withlastwarn('').

lastwarn('new_msgstr', 'new_msgid')sets the last warning message and its identifier to new character vectorsnew_msgstrandnew_msgid, respectively. Subsequent invocations oflastwarnreturn the new warning message and message identifier.

[msgstr, msgid] = lastwarn('new_msgstr', 'new_msgid')returns the last warning message and its identifier, also changing these values so that subsequent invocations oflastwarnreturn the message and identifier specified bynew_msgstrandnew_msgid, respectively.

Examples

Write a short function that generates a warning message. At the start of the function, enable any warnings that have a message identifier calledTestEnv:InvalidInput:

function myfun(p1) warning on TestEnv:InvalidInput; exceedMax = find(p1 > 5000); if any(exceedMax) warning('TestEnv:InvalidInput', ... '%d values in the "%s" array exceed the maximum.', ... length(exceedMax), inputname(1)) end

Pass an invalid value to the function:

dataIn = magic(10) - 2; myfun(dataIn) Warning: 2 values in the "dataIn" array exceed the maximum. > In myfun at 4

Uselastwarnto determine the message identifier and warning message for the operation:

[warnmsg, msgid] = lastwarn warnmsg = 2 values in the "dataIn" array exceed the maximum. msgid = TestEnv:InvalidInput

Tips

lastwarndoes not return warnings that are reported during the parsing of MATLAB commands. (Warning messages that include the failing file name and line number are parse-time warnings.)

Introduced before R2006a

Was this topic helpful?