Main Content

Obtain Cumulative Coverage for Reusable Subsystems

This example shows how to create and view cumulative coverage results for a model with a reusable subsystem or a reusable Stateflow™ chart.

Simulink® Coverage™ provides cumulative coverage for multiple instances of identically configured:

  • Subsystems

  • Stateflow objects, such as charts or library-linked subcharts

To obtain cumulative coverage, add the individual coverage results programmatically or simulate the model multiple times by using the Run button. You can get cumulative coverage results for multiple instances across models and test harnesses by adding the individual coverage results. For more information about cumulative coverage, see Cumulative Coverage Data.

Open Example Model

Open the slvnvdemo_cv_mutual_exclusion model.

modelName = "slvnvdemo_cv_mutual_exclusion";

The slvnvdemo_cv_mutual_exclusion model has two instances of a reusable subsystem, Subsystem 1 and Subsystem 2.

Analyze Decision Coverage

Create a Simulink.SimulationInput object for the model.

simIn = Simulink.SimulationInput(modelName);

Enable decision coverage analysis.

simIn = setModelParameter(simIn,"CovEnable","on");
simIn = setModelParameter(simIn,"CovMetricStructuralLevel","Decision");

Enable coverage for the entire system by using the CovScope parameter.

simIn = setModelParameter(simIn,"CovScope","EntireSystem");

Enable the CovSaveSingleToWorkspaceVar parameter, and set the name of the coverage data object to covData.

simIn = setModelParameter(simIn,"CovSaveSingleToWorkspaceVar","on");
simIn = setModelParameter(simIn,"CovSaveName","covData");

Simulate the model by using covInput as the input to sim. The simulation output object contains the coverage data in a property that has the same name as the CovSaveName parameter. For this example, use covData.

simOut = sim(simIn);
covData = simOut.covData;

Extract and View Subsystem Coverage Results

Extract the coverage data for Subsystem 1 using the cvdata.extract method. Then create an HTML report for Subsystem 1 by using cvhtml.

covDataSubsys1 = extract(covData,modelName+"/Subsystem 1");

The report indicates that decision coverage is 50% for Subsystem 1. The true condition for enable logical value is not satisfied.

Extract and view the coverage data for Subsystem 2.

covDataSubsys2 = extract(covData,modelName+"/Subsystem 2");

The report indicates that decision coverage is 50% for Subsystem 2. The false condition for enable logical value is not satisfied.

Generate Aggregated Subsystem Coverage Report

Combine the coverage results for the two simulations by using the + operator.

cumulativeCovData = covDataSubsys1 + covDataSubsys2;

% Create an HTML report for the cumulative decision coverage of |Subsystem
% 1| and |Subsystem 2|.


Even though the two different decision outcomes are satisfied by different instances of the subsystem, the aggregated report combines the data from the two simulations and reports 100% decision coverage.

Compare these results with the original coverage data, before using extract.


Because the coverage report for the system analysis displays Subsystem 1 and Subsystem 2 independently, it reports 50% decision coverage for the system, with 2 out of 4 total decision outcomes satisfied. When you analyze the entire system, the coverage report does not look for multiple instances of identical systems or Stateflow charts. If you want to combine the coverage data for identical subsystems, you must do it manually.

Alternatively, you can use Simulink Test™ to aggregate unit-level coverage data into the system level. See Aggregate Unit-Level Coverage Data into Top-Level Model Coverage.