warning

Displays a warning message and records it, without stopping the program.

Category: Programming > Functions

Syntax

warning(msg)

Shows Warning: msg and continues.

warning(msg, A1, ..., An)

Formats the message like sprintf using the extra arguments.

warning(id, msg, A1, ..., An)

Also attaches an identifier of the form component:mnemonic, so the warning can be recognized or controlled.

warning(state, id)

Turns the warning with identifier id (or 'all') on or off.

Description

Unlike error, a warning prints and the program carries on. The most recent warning is remembered and can be read with lastwarn, which returns its message and identifier. A call is treated as having an identifier only when the first argument looks like one: it contains a colon, has no spaces and is followed by a message.

In MATLAB the on, off and query forms control which warnings are shown, and 'error' turns a warning into an error.

Input arguments

msg
Message text, with sprintf conversions such as %d or %s.
id
Identifier, two or more name parts separated by colons and containing no spaces.
state
'on', 'off' or 'query'.

Example

Formatted warning with an identifier

warning('demo:size', 'Matrix is %d-by-%d', 3, 4)
[msg, id] = lastwarn
Output
msg = 'Matrix is 3-by-4'
id = 'demo:size'
Warning: Matrix is 3-by-4

Notes

In MatLivre the 'on', 'off' and 'query' forms are accepted but have no effect: warnings are always shown, nothing is suppressed, and 'query' returns no state.

See also

lastwarn error sprintf MException disp