%% Extract Saturation on Integer Overflow Data
% This example shows how to use |overflowsaturationinfo| to extract
% saturation on integer overflow data for a |MinMax| block from a |cvdata|
% object.

% Copyright 2020 The MathWorks, Inc.

%%
% Move the current MATLAB&reg; directory to the location that contains the
% example files.

openExample('slcoverage/ExtractSaturationOnIntegerOverflowDataExample');

%% Generate coverage data
% Open the model and set coverage settings.

load_system('slcoverage_fuelsys');

%%
% Create a |Simulink.SimulationInput| object to change configuration
% parameters without modifying the model.

covSet = Simulink.SimulationInput('slcoverage_fuelsys');

%%
% Turn on coverage analysis and configure Simulink(R) to save the coverage
% data in a separate |cvdata| object in the workspace.

covSet = covSet.setModelParameter('CovEnable','on');
covSet = covSet.setModelParameter('CovSaveSingleToWorkspaceVar','on');
covSet = covSet.setModelParameter('CovSaveName','covData');
covSet = covSet.setModelParameter('CovScope','EntireSystem');

%%
% Enable collection of saturation on integer overflow Coverage and simulate
% the model by calling |sim| with the |SimulationInput| object as the
% input.

covSet = covSet.setModelParameter('CovMetricSaturateOnIntegerOverflow','on');
simOut = sim(covSet);

%% Extract saturation on integer overflow results
% Get the block handle to the MinMax block using |get_param| and then get
% the saturation on integer overflow results.

blockHandle = get_param(['slcoverage_fuelsys/',...
    'Engine Gas Dynamics/Mixing & Combustion/MinMax'],'Handle');
saturationResults = overflowsaturationinfo(covData,blockHandle)
percentSaturationCoverage = 100 * saturationResults(1)/saturationResults(2)

%%
% One out of two saturation on integer overflow decision outcomes were
% satisfied for the |MinMax| block in the |Mixing & Combustion| subsystem,
% so it received 50% saturation on integer overflow coverage.