Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

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 = lastwarn returns the last warning message generated by MATLAB®, regardless of its display state.

[msgstr, msgid] = lastwarn returns the last warning in msgstr and its message identifier in msgid. If the warning was not defined with an identifier, lastwarn returns an empty character vector for msgid.

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

lastwarn('new_msgstr', 'new_msgid') sets the last warning message and its identifier to new character vectors new_msgstr and new_msgid, respectively. Subsequent invocations of lastwarn return 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 of lastwarn return the message and identifier specified by new_msgstr and new_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 called TestEnv: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

Use lastwarn to determine the message identifier and error message for the operation:

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

Related Examples

More About

collapse all

Tips

lastwarn does 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.)

See Also

|

Introduced before R2006a

Was this topic helpful?