%% Viewing Signals in Model Reference Instances
% This example shows how to use the Simulation Data Inspector to view
% and analyze signals in referenced models.  The Simulation Data Inspector
% relies on signal logging so this example shows how to configure your
% model to log signals in referenced models. 

%   Copyright 2013-2019 The MathWorks, Inc.


%%
% origdir = pwd;
evalc('slexMultiInstanceModelStart;');

%% Configure Signals to Log in Referenced Models
% The first task in setting up a referenced model to view signals using the
% Simulation Data Inspector is to configure that model for logging. To do that,
% edit the referenced model and mark the signals for logging. Four signals are already marked for logging in the referenced model.
% To mark the |raw_output| signal for logging, select the signal. Then, on
% the *Simulation* tab, select *Log Signals*.

%%
%
% <<../mdlref_sdi_loggingselect_new.png>>
%

%%
% Once the signals to log have been selected in the referenced model, the next
% step is to select which signals to actually log from the topmost model
% in the hierarchy. In the top model, on the *Modeling* tab, click *Model Settings*.

%%
%
% <<../mdlref_sdi_configuredatalogging_new.png>>
%

%%
% On the *Data Import/Export* pane, click the *Configure Signals to Log...* button to
% activate the Simulink Signal Logging Selector dialog.

%%
%
% <<../mdlref_sdi_configparamsdataimportexport_new.png>>
%

%%
% 
% <<../mdlref_sdi_sigselectordialog_new.png>>
%

%%
% There are two Logging modes that can be used when logging signals in 
% referenced models.  The first is *Log all signals as specified in model*, which
% will honor any logging settings that were made in referenced models.  If
% you need to override any of those settings, such as selecting only a
% subset of logged signals, then you should use the *Override signals*
% mode.  In this mode, you are given the ability to select a subset of
% loggable signals and configure their properties, such as decimation and
% name.  

%% Viewing and Analyzing Logged Signals Using the Simulation Data Inspector
% Once logging has been configured for a model hierarchy and all changes 
% to the models have been saved, the model can be
% simulated.  The simulation creates a Dataset object in the base
% workspace. You can analyze and view the logged data in this object using
% standard MATLAB(R) tools.  In addition, Simulink(R) provides the Simulation
% Data Inspector for viewing and analyzing data created from simulations.
% 
% With the Simulation Data Inspector, you can:
%
% * View signal traces for any logged signals
% * Visually compare signal values for any logged signals
% * Compare signal values collected over multiple simulations
%

%%
% For example, you can use the Simulation Data Inspector to view the |output| signal of 
% two of the instances of the model |LimitedCounter|.  In the Simulation Data Inspector, you
% can see that |CounterA| increases more rapidly than |CounterB|. This behavior is expected
% because the Pulse Generator driving this model is running at a faster rate.

%%
% 
% <<../mdlref_sdi_datainspector_new.png>>
% 

%%
% You can configure what to display in the inspect table by
% clicking the gear icon at the right top of the table.

%%
%
% <<../mdlref_sdi_datainspector_new_2.png>>
%

%%
% Another useful tool that the Simulation Data Inspector provides is the ability to
% compare multiple simulation runs.  This functionality can help you
% understand how changes to your model affect results.  For example,
% you can change the value of the upper limit of the counter models to 8 and 
% simulate the model again to see how that affects output values. To change the
% value, double click on the block |MultiInstanceModelExample/upper| and modify
% the Value parameter to 8.

set_param('MultiInstanceModelExample/upper','Value','8');
open_system('MultiInstanceModelExample');


%%
% Now if you compare the results from the first simulation for the output of
% the |CounterA| instance of |LimitedCounter|, you see that the upper limit
% of this signal has changed from 10 to 8, as expected. Because the tolerances are set to zero,
% the red octagon next to each signal indicates that the values have changed between runs.

%%
% 
% <<../mdlref_sdi_datainspector_comp_new.png>>
%

close_system('MultiInstanceModelExample', 0);
close_system('LimitedCounter', 0);
evalc('slproject.closeCurrentProject;');
% cd(origdir);
