Main Content

Programming Run-Time Errors and Warnings

Use the assert construct to implement run-time error and warning messages for a custom block. In the component file, you specify the condition to be evaluated, as well as the error message to be output if this condition is violated. When the custom block based on this component file is used in a model, it will output this message if the condition is violated during simulation. The optional Action attribute of the assert construct specifies whether simulation stops when the predicate condition is violated, continues with a warning, or ignores the violation.

The following component file implements a variable resistor, where input physical signal R supplies the resistance value. The assert construct checks that this input signal is greater than or equal to zero:

component MyVariableResistor
% Variable Resistor
% Models a linear variable resistor. The relationship between voltage V
% and current I is V=I*R where R is the numerical value presented at the
% physical signal port R. If this signal becomes negative, simulation
% errors out.

    R = { 0.0, 'Ohm' };

    p = foundation.electrical.electrical; % +:left
    n = foundation.electrical.electrical; % -:right

    i = { 0, 'A' };
    v = { 0, 'V' };

    i : p.i -> n.i;

    assert( R >= 0, 'Negative resistance is not modeled' );
    v == p.v - n.v;
    v == i*R;


If a model contains this Variable Resistor block, and signal R becomes negative during simulation, then simulation stops and the Simulation Diagnostics window opens with a message similar to the following:

At time 3.200000, an assertion is triggered. Negative resistance is not modeled.
The assertion comes from:
Block path: dc_motor1/Variable Resistor
Assert location: between line: 29, column: 14 and line: 29, column: 18 in file:

The error message contains the following information:

  • Simulation time when the assertion got triggered

  • The message string (in this example, Negative resistance is not modeled)

  • An active link to the block that triggered the assertion. Click the Block path link to highlight the block in the model diagram.

  • An active link to the assert location in the component source file. Click the Assert location link to open the Simscape™ source file of the component, with the cursor at the start of violated predicate condition. For Simscape protected files, the Assert location information is omitted from the error message.

See the assert reference page for syntax specifics and more examples.

Related Topics