Last warning message
msgstr
= lastwarn
[msgstr, msgid] = lastwarn
lastwarn('new_msgstr')
lastwarn('new_msgstr', 'new_msgid')
[msgstr, msgid] = lastwarn('new_msgstr',
'new_msgid')
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.
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