| Simulink® Verification and Validation™ | ![]() |
| On this page… |
|---|
Types of Model Coverage in Embedded MATLAB Function Blocks Creating a Model with Embedded MATLAB Function Block Decisions |
This section describes the model coverage that an Embedded MATLAB Function block receives.
Note Model coverage is available to you only if you have a Simulink Verification and Validation software license. |
During simulation, the following Embedded MATLAB Function block function statements are tested for decision coverage:
Function header — Decision coverage is 100% if the function or subfunction is executed.
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, 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 equivalent loop condition evaluates to true at least once, and false at least once.
During simulation, the following logical conditions are tested for condition coverage and MCDC coverage in the Embedded MATLAB Function block function:
if statement conditions
while statement conditions, if present
In this topic you use an example model to examine model coverage of an Embedded MATLAB Function block. The following model contains a single Embedded MATLAB Function block with output data sent to a Scope block.

Double-click the Embedded MATLAB Function block to specify its program content as shown.

The run_intersect_test Embedded MATLAB Function block contains two functions. The top-level function, run_intersect_test, sends the coordinates for two rectangles, one fixed and the other moving, as arguments to the subfunction rect_intersect, which tests for intersection between the two. The origin of the moving rectangle increases by 1 in the x and y directions with each time step.
The coordinates for the origin of the moving test rectangle are represented by persistent data x1 and y1, which are both initialized to -1. For the first sample, x1 and y1 are both incremented to 0. From then on, the progression of rectangle arguments during simulation is as follows:

The fixed rectangle is shown in bold with a lower left origin of (2,4) and a width and height of 2. At time t = 0, the first test rectangle has an origin of (0,0) and a width and height of 2. For each succeeding sample, the origin of the test rectangle is incremented by (1,1). The rectangles at sample times t = 2, 3, and 4 intersect with the test rectangle.
The subfunction rect_intersect checks to see if its two rectangle arguments intersect. Each argument consists of coordinates for the lower left corner of the rectangle (origin), and its width and height. x values for the left and right sides and y values for the top and bottom are calculated for each rectangle and compared in nested if-else decisions. The function returns a logical value of 1 if the rectangles intersect and 0 if they do not.
Scope output during simulation, which plots the return value against the sample time, confirms the intersecting rectangles for sample 2, 3, and 4 as shown.

Model coverage reports are generated automatically after a simulation if you specify them. See Creating and Running Test Cases for instructions on how to specify a model coverage report.
When simulation is finished, the model coverage report appears in a browser window. After the summary for the model, the Details section of the model coverage report reports on each of the parts of the model. Model coverage for the parts of the example model in Creating a Model with Embedded MATLAB Function Block Decisions appears in the following model-block-function order.
Model: | intersecting_rectangles |
Block: | Embedded MATLAB Function |
Function: | run_intersect_test |
Decision Lines: | 1: function out = rect_intersect_test |
6: if isempty(x1) | |
14: function out = rect_intersect(rect1, rect2) | |
27: if (top1 < bottom2 || top2 < bottom1) | |
30: if (right1 < left2 || right2 < left1) |
The following subtopics examine the model coverage report for the example model in reverse function-block-model order. Reversing the order helps you make sense of the summary information that appears at the top of each section.
Model coverage for the Embedded MATLAB Function block function run_intersect_test is reported under the linked name of the function. Clicking this link opens the function in the Embedded MATLAB Editor. Following the linked function name is a link to the model coverage report for the parent Embedded MATLAB Function block of run_intersect_test.

The top half of the report for the function summarizes its model coverage results as shown. The coverage metrics for run_intersect_test include decision, condition, and MCDC coverage. These metrics are best understood by examining the code listing for run_intersect_test that follows.

Lines with coverage elements are marked by a highlighted line number in the listing. Line 1 receives decision coverage on whether the top-level function run_intersect_test is executed. Line 6 receives decision coverage for its if statement. Line 14 receives decision coverage on whether the subfunction rect_intersect is executed. Lines 27 and 30 receive decision, condition, and MCDC coverage for their if statements and conditions. Each of these lines is the subject of a report that follows the listing.
Notice that the condition right1 < left2 in line 30 is highlighted in red. This means that this condition was not tested for all of its possible outcomes during simulation. Exactly which of the outcomes was not tested is answered by the report for the decision in line 30.
The following subtopics display the coverage for each decision line of run_intersect_test. The coverage for each line is titled with the line itself, which is linked to display the function with the line highlighted.
Coverage for Line 1. The coverage metrics for line 1 appear below the listing for the function run_intersect_test.

The first line of every function receives coverage analysis indicative of the decision to run the function in response to a call. Coverage for run_intersect_test indicates that it executed during testing.
Coverage for Line 6. The coverage metrics for line 6 appear below the coverage metrics for line 1.

The Decisions analyzed table indicates that the decision in line 6, if isempty(x1), executed a total of eight times. The first time it executed, the decision evaluated to true, enabling run_intersect_test to initialize the values of its persistent data. The remaining seven times the decision executed, it evaluated to false. Because both possible outcomes occurred, decision coverage is 100%.
Coverage for Line 14. The coverage metrics for line 14 appear below the coverage metrics for line 6.

This table indicates that the subfunction rect_intersect executed during testing.
Coverage for Line 27. Coverage metrics for line 27 appear below the coverage metrics for line 14.

The Decisions analyzed table indicates that there are two possible outcomes for the decision in line 27: true and false. Five of the eight times it was executed, the decision evaluated to false, and the remaining three times, it evaluated to true. Because both possible outcomes occurred, decision coverage is 100%.
The Conditions analyzed table sheds some additional light on the decision in line 27. Because this decision consists of two conditions linked by a logical OR (||) operation, only one condition must evaluate true for the decision to be true. If the first condition evaluates to true, there is no need to evaluate the second condition. The first condition, top1 < bottom2, was evaluated eight times, and was true twice. This means that it was necessary to evaluate the second condition only six times. In only one case was it true, which brings the total true occurrences for the decision to three, as reported in the Decisions analyzed table.
MCDC coverage looks for decision reversals that occur because one condition outcome changes from T to F or from F to T. The MC/DC analysis table identifies all possible combinations of outcomes for the conditions that lead to a reversal in the decision. The character x is used to indicate a condition outcome that is irrelevant to the decision reversal. Decision-reversing condition outcomes that are not achieved during simulation are marked with a set of parentheses. There are no parentheses, therefore all decision-reversing outcomes occurred and MCDC coverage is complete for the decision in line 27.
Coverage for Line 30. Coverage metrics for line 30 appear below the coverage metrics for line 27.

The line 30 decision, if (right1 < left2 || right2 < left1), is nested in the if statement of the line 27 decision and is evaluated only if the line 27 decision is false. Because the line 27 decision evaluated false five times, line 30 is evaluated five times, three of which were false. Because both the true and false outcomes were achieved, decision coverage for line 30 is 100%.
Because line 30, like line 27, has two conditions related by a logical OR operator (||), condition 2 is tested only if condition 1 is false. Because condition 1 tests false five times, condition 2 is tested five times. Of these, condition 2 tests true two times and false three times, which accounts for the two occurrences of the true outcome for this decision.
Because the first condition of the line 30 decision does not test true, both outcomes did not occur for that condition and the condition coverage for the first condition is highlighted with a rose color. MCDC coverage is also highlighted in the same way for a decision reversal based on the true outcome for that condition.
Coverage for run_intersect_test. The metrics that summarize coverage for the entire run_intersect_test function are reported prior to its listing and are repeated here as shown.

The results summarized in the coverage metrics summary can be expressed in the following conclusions:
There are eight decision outcomes reported for run_intersect_test in the line reports: one for line 1 (executed), two for line 6 (true and false), one for line 14 (executed), two for line 27 (true and false), and two for line 30 (true and false). The decision coverage for each line shows 100% decision coverage. This means that decision coverage for run_intersect_test is eight of eight possible outcomes, or 100%.
There are four conditions reported for run_intersect_test in the line reports. Lines 27 and 30 each have two conditions, and each condition has two condition outcomes (true and false), for a total of eight condition outcomes in run_intersect_test. All conditions tested positive for both the true and false outcome except for the first condition of line 30 (right1 < left2). This means that condition coverage for run_intersect_test is seven of eight, or 88%.
The MCDC coverage tables for decision lines 27 and 30 each list two cases of decision reversal for each condition, for a total of four possible reversals. Only the decision reversal for a change in the evaluation of the condition right1 < left2 of line 30 from true to false did not occur during simulation. This means that three of four, or 75% of the possible reversal cases were tested for during simulation, for a coverage of 75%.
The model coverage report for the block Embedded MATLAB Function shows that it has no decisions of its own apart from its function. However, it does repeat the summary information for its function run_intersect_test as coverage for its descendent objects, as shown.

Because there are no additional coverage objects in the model apart from the Embedded MATLAB Function block, the remaining report for the model intersecting_rectangles also repeats the preceding coverage for descendent objects, as shown.

![]() | Using Model Coverage Commands for Referenced Models | Customizing the Model Advisor | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |