Main Content

Automating Model Coverage Tasks

You can automate coverage analysis in a script by using the Simulink Coverage functions and classes. For example, you might want to collect coverage data by simulating the same model with different model parameters. Instead of changing parameters manually, you can run the simulations and collect the coverage data in a loop.

Collect Coverage Data Using a Script

This example shows how to collect coverage data using sim.

Load the Model

First, load the model and the system you want to analyze into memory.

load_system('slvnvdemo_ratelim_harness');

Set Coverage Settings

Set up the coverage parameters using one of the methods described in sim, such as a simulation input, parameter structure, or name-value pairs. For example, in order to use a structure of parameters, set up a structure whose fields are names of configuration parameters, and whose values are the corresponding values of those parameters.

paramStruct.CovEnable = 'on';
paramStruct.CovMetricStructuralLevel = 'Decision';
paramStruct.CovSaveSingleToWorkspaceVar = 'on';
paramStruct.CovSaveName = 'covData';
paramStruct.CovScope = 'Subsystem';
paramStruct.CovPath = '/Adjustable Rate Limiter';
paramStruct.StartTime = '0.0';
paramStruct.StopTime = '2.0';

For an example that uses the Simulink.SimulationInput object, see Record Coverage in Parallel Simulations by Using Parsim.

Set up a Test and Simulate the Model

The example model uses input values that are defined in the MATLAB® workspace. The values used in this example are defined in a data file called within_lim.mat. You can use load to load the file into the workspace.

load within_lim.mat;

Simulate the model using sim with paramStruct as an additional input to collect coverage data using the specified parameters.

simOut = sim('slvnvdemo_ratelim_harness',paramStruct);

Generate a Coverage Report

You can generate an HTML report to view the coverage data that your simulation generates with cvhtml. The first input is the name of the coverage report that will be saved in the current directory. The second input is the cvdata object that was saved to the workspace based on the model parameters CovSaveSingleToWorkspaceVar and CovSaveName.

You can generate the report without automatically opening it by using the flag '-sRT=0' as the third input to cvhtml.

cvhtml('covReport',covData,'-sRT=0');

Save Coverage Data

Use cvsave to save the coverage results. The first input is the name of the coverage data file, and the second input is the cvdata object.

cvsave('covdata',covData);

Close the Model

Exit the coverage environment by using cvexit and close the model by using close_system. A second input of 0 indicates that you do not want to save model before closing.

cvexit
close_system('slvnvdemo_ratelim_harness',0);

Differences between sim and the Run Button

When you run a simulation with coverage enabled by using the Run button, the coverage report opens automatically and Coverage Highlighting is enabled by default. When you run a simulation programmatically by using sim, the coverage report does not open and Coverage Highlighting is not enabled.

  • To see coverage results displayed using model highlighting, use cvmodelview.

  • To see a coverage report, use cvhtml.

  • To open the Results Explorer, open the model in Simulink®. In the Apps tab, click Coverage Analyzer. Then click Results Explorer.

For another detailed example, see Command Line Verification Tutorial.

Collecting Coverage with Simulink Test

If you have a Simulink Test™ license, you can use the Test Manager to collect coverage data. For more information, Run a Test Case and Collect Coverage (Simulink Test).

See Also

| | | |

Related Topics