MATLAB: Programmatically Add Coverage Filter for MATLAB function inside MATLAB Function Block

Simulink Coverage

I would like to programmatically add a coverage filter for an arbitrary MATLAB function called from a MATLAB function block in my model. This can be done non-programmatically from the HTML report, by clicking on the "Justify or Exclude" link. How can we add a MATLAB function to the coverage filter from the command-line?

Best Answer

You can programmatically add a filter for a MATLAB function by creating a selector with the following command:
>> selector = slcoverage.BlockSelector(slcoverage.BlockSelectorType.BlockInstance, <matlab function name here>);
You can then use this selector to create a filter rule as follows:
>> filter = slcoverage.Filter;
>> filterRule = slcoverage.FilterRule(selector, 'Exclude from coverage', slcoverage.FilterMode.Exclude);
>> filt.addRule(rule);
Please also note that you can get all selectors associated with a MATLAB function by executing the following command after simulating the model for coverage:
>> selectors = slcoverage.Selector.allSelectors(<matlab function name here>);
More information on this command can be found here: