%% Add Code Selector Rules to a Filter
% This example shows how to select custom C or C++ code for which you want
% to add a filter rule.

% Copyright 2020 The MathWorks, Inc.

%%
% Load the model.

modelName = 'slcovCCallerExample';
Simulink.importExternalCTypes('my_func.h','EnumClass','dynamic');
load_system(modelName)

%%
% Configure coverage settings using a |Simulink.SimulationInput| object.

covSet = Simulink.SimulationInput(modelName);
covSet = covSet.setModelParameter('CovEnable','on');
covSet = covSet.setModelParameter('CovMetricStructuralLevel','MCDC');
covSet = covSet.setModelParameter('CovSFcnEnable','on');
covSet = covSet.setModelParameter('CovSaveSingleToWorkspaceVar','on');
covSet = covSet.setModelParameter('CovSaveName','covData');
covSet = covSet.setModelParameter('SimAnalyzeCustomCode','on');

%%
% Simulate the model using |covSet| object as the input.

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

%%
% Create a selector object to filter the custom C function |timesK|.

sel = slcoverage.CodeSelector(slcoverage.CodeSelectorType.Function, 'my_func.c', 'timesK');
       
%%
% Create a filter object and create a rule based on the selector, then add
% the rule to the filter.

filt = slcoverage.Filter;
rule = slcoverage.FilterRule(sel,'Tested elsewhere',...
           slcoverage.FilterMode.Exclude);
addRule(filt,rule);
setFilterName(filt,'Code Filter')
       
%%
% Save the filter as |codefilter| and add it to the |cvdata| object for
% |my_func.c|. Because the coverage data is stored in a |cv.cvdatagroup|
% object, use the |get| method to set the property.

save(filt,'codefilter');
covData.get('my_func.c').filter = 'codefilter';

%%
% Generate a coverage report.

cvhtml('codeCovReport',covData)

%%
% Review the report. Under *Custom Code File(s)*, click |my_func.c| and
% find the filter rule that you added under Objects Filtered from Coverage
% Analysis.

%%
%
% <<../codeselectorCoverageAfter.png>>
%
