Main Content

Model Coverage for MATLAB Functions

Simulink® Coverage™ analyzes your model and reports model coverage data when you simulate a model with coverage enabled. If your model contains MATLAB® code, such as inside a MATLAB Function block or a call to an external MATLAB function, then Simulink Coverage analyzes that code for the metrics that you select.

Collecting Model Coverage for MATLAB Functions

To analyze your MATLAB Function block or external MATLAB function for coverage, you must first ensure that the MATLAB code inside the block or function is compatible with code generation. For more information about configuring MATLAB code for code generation, see Workflow for Preparing MATLAB Code for Code Generation (MATLAB Coder).

For example, consider the following if statement:

if (x > 0 || y > 0)
	reset = 1;

The if statement contains a decision with two conditions (x > 0 and y > 0). Simulink Coverage analyzes each decision and condition during the simulation of the model and reports how many times each outcome occurs. For example, if the input signal for x is true at every simulation time step, it reports 50% condition coverage for that condition because the false case does not occur.

When collecting coverage for MATLAB functions, consider:

  • If your model contains a MATLAB Function block, and the function contains code to which your selected coverage metrics apply, then the MATLAB Function block receives coverage analysis.

  • To collect coverage for functions in external MATLAB files, in the Coverage pane of the Configuration Parameters dialog box, under Include in analysis, select MATLAB files.

  • To collect coverage for Simulink Design Verifier™ functions:

    In the Coverage pane of the Configuration Parameters dialog box, under Other metrics, select Objectives and Constraints.

Types of Model Coverage for MATLAB Functions

The types of model coverage that Simulink Coverage analyzes for MATLAB functions are:

Decision Coverage

During simulation, Simulink Coverage analyzes these MATLAB statements and reports them as decision coverage:

  • Function header — Decision coverage is 100% if the function or local function is executed at least once.

  • if — Decision coverage is 100% if the if expression evaluates to true at least once and false at least once.

  • switch — Decision coverage is 100% if every switch case is taken at least once, including the fall-through case.

  • for — Decision coverage is 100% if the equivalent loop condition evaluates to true at least once and false at least once.

  • while — Decision coverage is 100% if the loop condition evaluates to true at least once and false at least once.

Condition and MCDC Coverage

When you collect condition and MCDC coverage, Simulink Coverage analyzes if statement conditions and logical expressions in assignment statements.

Logical expressions are expressions that contain a logical operator, such as the logical AND (&&) or the logical OR (||). Model coverage does not analyze simple logical assignments such as:

b = true;
Or single condition assignment statements, such as:
b = a < 1;

Simulink Design Verifier Coverage

These MATLAB functions are active in code generation and in Simulink Design Verifier:

When you select the Objectives and Constraints coverage metric in the Coverage pane of the Configuration Parameters dialog box, Simulink Coverage analyzes these functions.

Each of these functions evaluates an expression, such as sldv.test(expr), where expr is a valid Boolean MATLAB expression. Simulink Design Verifier coverage measures the number of time steps that the expression expr evaluates to true.

If expr is true for at least one time step, Simulink Design Verifier coverage for that function is 100%. Otherwise, the reported coverage for that function is 0%.

For an example of coverage data for Simulink Design Verifier functions in a coverage report, see Simulink Design Verifier Coverage.

Saturation on Integer Overflow Coverage

When you select the Saturate on integer overflow parameter, Simulink Coverage analyzes statements in the MATLAB function that contain the integer datatype.

Saturate on integer overflow coverage records the number of times the statement saturates on integer overflow. A test case achieves full coverage when the logical statement saturates on integer overflow at least once and does not saturate at least once.

Relational Boundary Coverage

You can collect relational boundary coverage for MATLAB Function blocks that contain at least one relational operation.

If the MATLAB Function block calls functions containing relational operations multiple times, the relational boundary coverage reports a cumulative result over each instance where the function is called. If a relational operation in the function uses operands of different types in the different calls, relational boundary coverage uses tolerance rules for the stricter operand type. For example, if a relational operation uses int32 operands in one call and double operands in another call, relational boundary coverage uses tolerance rules for double operands.

For information on the tolerance rules and the order of strictness of types, see Relational Boundary Coverage.

Due to run-time optimizations, dead logic using hard-coded constant inputs is not analyzed for relational boundary coverage. For example, consider the function:

function out = myFun(x,y)
if nargin < 2
    y = 3;
This function can take one or two input arguments and sets a default if the caller did not provide the second argument. If your model always provides two input arguments when calling this function, the contents of the if statement is dead logic because the if statement is false at every time step. In this case, Simulink Coverage does not analyze the statement for relational boundary coverage.

See Also

Related Topics