%% Collect Coverage Data Using a Script
% This example shows how to collect coverage data using |sim|.

%   Copyright 1990-2019 The MathWorks, Inc.

%% 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
% |<docid:simulink_ref#bvfe92n 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 <docid:simulink_ref#bvn9b0e
% Simulink.SimulationInput> object, see
% <docid:slcoverage_ug#example-slvnvdemo_parsim 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&reg;
% 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);

%%
% For a complete list of Simulink Coverage configuration parameters, see
% <docid:slcoverage_ref#buwlv0o-1 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 |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);
