assert(cond,msg) throws
an error and displays the error message, msg, if cond is
false.

assert(cond,msg,A1,...,An) displays
an error message that contains formatting conversion characters, such
as those used with the MATLAB^{®}sprintf function,
if cond is false. Each conversion character in msg is
converted to one of the values A1,...,An.

assert(cond,msgID,msg) throws
an error, displays the error message, msg, and
includes an error identifier on the exception, if cond is
false. The identifier enables you to distinguish errors and to control
what happens when MATLAB encounters the errors.

assert(cond,msgID,msg,A1,...,An) includes
an error identifier on the exception and displays a formatted error
message.

Assert that the value, x, is greater
than a specified minimum value.

minVal = 7;
x = 26;
assert(minVal < x)

The expression evaluates as true, and the assertion passes.

Assert that the value of x is between
the specified minimum and maximum values.

maxVal = 13;
assert((minVal < x) && (x < maxVal))

Assertion failed.

The expression evaluates as false. The assertion fails and MATLAB throws
an error.

Expected Data Type

Assert that the product of two numbers is a
double-precision number.

a = 13;
b = single(42);
c = a*b;
assert(isa(c,'double'),'Product is not type double.')

Product is not type double.

Enhance the error message to display the data type of c.

assert(isa(c,'double'),'Product is type %s, not double.',class(c))

Product is type single, not double.

Expected Code Conditions

Use the assert function to test for
conditions that should not happen in normal code execution. If the
coefficients are numeric, the computed roots should be numeric. A
quadratic equation using the specified coefficients and computed roots
should be zero.

function x = quadraticSolver(C)
validateattributes(C,{'numeric'},{'size',[1 3]})
a = C(1);
b = C(2);
c = C(3);
x(1) = (-b+sqrt(b^2-4*a*c))/(2*a);
x(2) = (-b-sqrt(b^2-4*a*c))/(2*a);
assert(isnumeric(x),'quadraticSolver:nonnumericRoots',...'Computed roots are not numeric')
y1 = a*x(1)^2+b*x(1)+c;
y2 = a*x(2)^2+b*x(2)+c;
assert(y1 == 0,'quadraticSolver:root1Error','Error in first root')
assert(isequal(y2,0),'quadraticSolver:root2Error','Error in second root')
end

Condition to assert, specified as a valid MATLAB expression.
If cond is false, the assert function
throws an error. cond can include relational operators
(such as < or ==) and logical
operators (such as &&, ||,
or ~). Use the logical operators and and or to
create compound expressions. MATLAB evaluates compound expressions
from left to right, adhering to operator precedence rules.

Example: a<0

Example: exist('myfunction.m','file')

msg — Information about assertion failure character vector

Information about the assertion failure, specified as a character
vector. This message displays as the error message. To format the
character vector, use escape sequences, such as \t or \n.
You also can use any format specifiers supported by the sprintf function,
such as %s or %d. Specify values
for the conversion specifiers via the A1,...,An input
arguments. For more information, see Formatting Text.

Note:
You must specify more than one input argument with assert if
you want MATLAB to convert special characters (such as \t, \n, %s,
and %d) in the error message.

Example: 'Assertion condition failed.'

A1,...,An — Numeric or character arrays scalar | vector | matrix | multidimensional array

Numeric or character arrays, specified as a scalar, vector,
matrix, or multidimensional array. This input argument provides the
values that correspond to and replace the conversion specifiers in msg.

msgID — Identifier for assertion failure character vector

Identifier for the assertion failure, specified as a character
vector. Use the message identifier to help identify the source of
the error or to control a selected subset of the errors in your program.

The message identifier includes a component and mnemonic.
The identifier must always contain a colon and follows this simple
format: component:mnemonic. The component and mnemonic fields
must each begin with a letter. The remaining characters can be alphanumerics
(A–Z, a–z, 0–9) and underscores. No whitespace
characters can appear anywhere in msgID. For
more information, see Message Identifiers.

When you issue an error, MATLAB captures information
about it and stores it in a data structure that is an object of the MException class.
You can access information in the exception object by using try/catch.
Or, if your program terminates because of an exception and returns
control to the Command Prompt, you can use MException.last.

If an assertion failure occurs within a try block, MATLAB does
not cease execution of the program. In this case, MATLAB passes
control to the catch block.