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.
This example shows how to collect coverage data using
Load the Model
First, load the model and the system you want to analyze into memory.
Set Coverage Settings
Set up the coverage parameters using one of the methods described in
, 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.
Simulate the model using
paramStruct as an additional input to collect coverage data using the specified parameters.
simOut = sim('slvnvdemo_ratelim_harness',paramStruct);
For a complete list of Simulink Coverage configuration parameters, see Coverage Settings.
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
You can generate the report without automatically opening it by using the flag
'-sRT=0' as the third input to
Save Coverage Data
cvsave to save the coverage results. The first input is the name of the coverage data file, and the second input is the
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.
simand 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.
For another detailed example, see Command Line Verification Tutorial.
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).