Throw error if condition false
assert(
displays an error message that contains formatting conversion characters, such
as those used with the MATLAB
®
cond
,
msg
,
A
)
sprintf
function, if
cond
is false. Each
conversion character in
msg
is converted to one of the values
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))
Error using assert Assertion failed.
The expression evaluates as false. The assertion fails and MATLAB throws an error.
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.')
Error using assert 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))
Error using assert Product is type single, not double.
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
cond
—
Condition to assert
Condition to assert, specified as a valid MATLAB expression. This expression must be logical or convertible to
a logical. 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
Information about the assertion failure, specified as a character vector or string scalar. This message displays as the error message. To format the message, 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.'
A
—
Replacement value
Value that replace the conversion specifiers in
msg
,
specified as a character vector, string scalar, or numeric scalar.
errID
—
Identifier for assertion failure
Identifier for the assertion failure, specified as a character vector or string scalar. Use the identifier to help identify the source of the error or to control a selected subset of the errors in your program.
The error identifier includes one or more
component
fields and a
mnemonic
field. Fields must be separated
with colon. For example, an error identifier with a component field
component
and a mnemonic field
mnemonic
is specified as
'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 white-space characters can
appear anywhere in
errID
. For more information, see
MException
.
Example:
'MATLAB:singularMatrix'
Example:
'MATLAB:narginchk:notEnoughInputs'
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.
Usage notes and limitations:
Generates specified error messages at compile time only if all input arguments are constants or depend on constants. Otherwise, generates specified error messages at run time.
If called with more than 1 argument, has no effect in standalone code even when run-time error detection is enabled. See Generate Standalone C/C++ Code That Detects and Reports Run-Time Errors (MATLAB Coder) .
To use the
assert
function to specify properties
of primary function inputs or set preconditions on primary function
inputs, see
Rules for Using assert Function
(MATLAB Coder)
.
backgroundPool
or accelerate code with Parallel Computing Toolbox™
ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment .
The
assert
function
supports GPU array input with these usage notes and limitations:
This function accepts GPU arrays, but does not run on a GPU.
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox) .
When an assertion fails, the error thrown includes the specific assertion that failed and the location in the code.
Ha hecho clic en un enlace que corresponde a este comando de MATLAB:
Ejecute el comando introduciéndolo en la ventana de comandos de MATLAB. Los navegadores web no admiten comandos de MATLAB.