Skip to Main Content Skip to Search
Product 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 = 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. See Message Identifiers and Warning Control in the MATLAB Programming Fundamentals documentation for more information on the msgid argument and how to use it.

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.

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

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

See Also

error | lasterr | lasterror | warning

  


» Learn more
» Download free kit
» Get trial software

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS