lastwarn

Returns or sets the most recent warning message.

Category: Programming > Functions

Syntax

msg = lastwarn

Returns the last warning message.

[msg, id] = lastwarn

Also returns its identifier.

lastwarn(msg, id)

Sets the stored message and identifier.

Description

Each warning issued through warning is remembered, even if it was switched off. Code can call lastwarn('') before an operation and read lastwarn afterwards to learn whether the operation warned, without parsing printed output.

Input arguments

msg
Message to store.
id
Identifier to store.

Output arguments

msg
Text of the last warning; empty if none was issued.
id
Identifier of the last warning.

Example

Detecting a warning

lastwarn('');
warning('Demo:low', 'Level is low (%d)', 3);
[msg, id] = lastwarn
Output
msg = 'Level is low (3)'
id = 'Demo:low'
Warning: Level is low (3)

See also

warning lasterr error MException