assert - Evaluate logical expression and stop simulation if false - MATLAB (original) (raw)
Main Content
Evaluate logical expression and stop simulation if false
Syntax
Description
assert(`expression`)
evaluates a logicalexpression
. Logical expressions evaluate to true
or false
. If the assert
statement evaluates tofalse
, simulation stops and returns an error.
assert(`expression`,`errmsg`)
returns the specified error message string (errmsg
) for the failedassert
statement. If you run the test in the Test Manager, the error message appears in the simulation log. If you run the test outside the Test Manager, the message appears in the Diagnostic Viewer.
Examples
If either h
or k
are not 0, thisassert
statement fails and simulation stops.
Tips
assert
statements can be used in the Test Sequence and Test Assessment blocks and in Stateflow® charts.assert
in Model blocks works in Normal mode, but not in Rapid Accelerator mode simulations.
Note
In Test Sequence blocks or Stateflow charts,[assert](assert.html)
statements are not supported for code generation and are ignored, so no error occurs if the assert condition fails during a Simulink® Real-Time™ simulation. However,[verify](verify.html)
statements are supported for Simulink Real-Time code generation and automatically log results for a test case in the Test Manager. The same logging behavior is available when using a Simulink Assert block.- When comparing floating-point data in
assert
statements, consider the precision limitations associated with floating-point numbers. If you need to use floating-point data, define a tolerance for the assessment. See Floating-Point Numbers. For example, instead ofassert(x == 5)
, assertx
within a tolerance of 0.001:
Version History
Introduced in R2015a