assert
Check that a condition is true.
📝Syntax
assert(condition)
assert(condition, message)
assert(condition, message, value)
assert(condition, identifier, message)
assert(condition, identifier, message, value)
[res, msg] = assert(...)
📥Input Arguments
Parameter Description
condition Logical or real numeric scalar or array to test. Every entry must be nonzero.
message Optional custom failure message. Format replacements are supported with following values.
identifier Optional error identifier used when the assertion raises an error.
value Optional value inserted in the message format.
📤Output Arguments
Parameter Description
res true if the assertion passes, false otherwise.
msg Assertion failure message, empty on success.
📄Description

assert raises an error when condition is false and no output is requested.

With outputs, assertion failures are returned as res and msg instead of being raised.

Use the asserts package for qualified assertion helpers, for example asserts.isequal(...).

💡Examples
Passing condition
assert(5 > 3);
Custom message
[res, msg] = assert(false, 'condition failed');
Formatted message
[res, msg] = assert(false, 'value %.2f', 1.234);
Error identifier
[res, msg] = assert(false, 'Nelson:asserts:example', 'condition failed');
🔗See Also
asserts.istrueasserts.isfalseasserts.isequalasserts.isapprox
🕔Version History
Version Description
1.0.0 initial version
2.0.0 added formatted messages, error identifiers and output mode
Edit this page on GitHub