Main Content

Programmatically Retrieve Coverage Details from Results

Analyze Coverage Data Using A Script

This example shows how to load, parse, and query coverage data using a script.

Load Coverage Data

Load the model, then restore saved coverage data from the file covdata.cvt using cvload. The data and test settings are retrieved in a cell array. The test settings are stored in a cvtest object that contains the parameters from the simulation that created the coverage data.

[savedSettings,savedData] = cvload('covdata');
savedData = savedData{1};

Extract Information from Coverage Data Objects

Retrieve coverage information from a block path or block handle by using decisioninfo. The output is a vector with the achieved and total outcomes for a single model object.

subsysCov = decisioninfo(savedData,...
                   'slvnvdemo_ratelim_harness/Adjustable Rate Limiter')
subsysCov =

     5     6

Determine the percentage coverage achieved by using decisioninfo.

percentCov = 100 * (subsysCov(1)/subsysCov(2))
percentCov =


Specify that you want to extract the decision coverage data for the switch block called Apply Limited Gain by using decisioninfo. This returns a structure which contains the decisions and outcomes.

[blockCov,desc] = decisioninfo(savedData, ...
         'slvnvdemo_ratelim_harness/Adjustable Rate Limiter/Apply limited gain');
descDecision = desc.decision;
outcome1 = desc.decision.outcome(1)
outcome2 = desc.decision.outcome(2)
outcome1 = 

  struct with fields:

               text: 'false (out = in3)'
     executionCount: 0
         executedIn: []
         isFiltered: 0
        isJustified: 0
    filterRationale: ''

outcome2 = 

  struct with fields:

               text: 'true (out = in1)'
     executionCount: 101
         executedIn: []
         isFiltered: 0
        isJustified: 0
    filterRationale: ''

From the decisioninfo output, you can see that the switch block called Apply Limited Gain was never false because the false case executionCount field has a value of 0. If this behavior is expected, and you did not intend to execute this case with your tests, you can add a filter rule to justify this missing coverage using the slcoverage.Filter class.

First, query for the block instance to be filtered, because we only need to filter the one block instance that received incomplete coverage, and not all instances of that block type. Then use the slcoverage.BlockSelector class with the BlockInstance selector type to designate one block instance for filtering.

id = getSimulinkBlockHandle('slvnvdemo_ratelim_harness/Adjustable Rate Limiter/Apply limited gain');
sel = slcoverage.BlockSelector(slcoverage.BlockSelectorType.BlockInstance,id);

Create a filter object and a filter rule using the slcoverage.Filter and slcoverage.FilterRule classes.

filt = slcoverage.Filter;
rule = slcoverage.FilterRule(sel,'Edge case',slcoverage.FilterMode.Justify);

Add the rule to the filter using the addRule method. Then save the new filter file with the save method.


Create a new cvdata object from the original object, and apply the filter file to it. Use decisioninfo on the filtered coverage data to see that there is now 100% decision coverage because the justified objectives are counted as satisfied.

FilteredData = savedData;
FilteredData.filter = 'blfilter';
newCov = decisioninfo(FilteredData,...
                   'slvnvdemo_ratelim_harness/Adjustable Rate Limiter')
percentNewCov = 100 * (newCov(1)/newCov(2))
newCov =

     6     6

percentNewCov =


Coverage Information Functions

After you collect coverage data, you can extract specific coverage information from the cvdata object by using the following functions. Use these functions to retrieve the specified coverage information for a block, subsystem, or Stateflow® chart in your model, or for the model itself.

You can turn on coverage highlighting on your Simulink model by using cvmodelview. You can also view the coverage report using cvhtml.

For an example that uses these functions, see Extracting Detailed Information from Coverage Data.

See Also

| | |

Related Topics