Documentation Center

  • Trials
  • Product Updates

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 the MATLAB® software.

[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 string for msgid.

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

lastwarn('new_msgstr', 'new_msgid') sets the last warning message and its identifier to new strings 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 strings 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 string for the operation:

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

More About

expand 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

|

Related Examples

Was this topic helpful?