%% Data Logging with Simulation Data Inspector (SDI)
% 
% This example shows how to use a Simulink(R) Real-Time(TM) log of signal 
% data and the Simulation Data Inspector.  Signals are logged during model 
% execution. At the end of the run, the Simulation Data Inspector interface 
% displays the signal. This example show how to get the signals from the 
% Simulation Data Inspectore interface by using the command line.
%
%% Open, Build, and Download the Model
% Open the model |<matlab:open_system(matlabroot,'toolbox','slrealtime','slrt_ex_soc_dist') slrt_ex_soc_dist>|.
% This model calibrates the control efforts through social distancing on an
% infectious disease outbreak. 
%
% Open the model.
% 
mdl = 'slrt_ex_soc_dist';
mdlOpen = 0;
systems = find_system('type', 'block_diagram');
if all(~strcmp(mdl, systems))
    mdlOpen = 1;
    open_system(fullfile(matlabroot,'toolbox','slrealtime','examples','slrt_ex_soc_dist.slx'));
end
%%
% Build the model and download to the target computer:
%
% * Configure for a non-Verbose build.
% * Build and download application.
%
set_param(mdl,'RTWVerbose','off');
rtwbuild(mdl);
%%
% * Close the model it is open.
%
if (mdlOpen)
    bdclose(mdl);
end
%% Run the Model to Evaluate Effects of No Social Distancing During the Outbreak 
% 
% Using the Simulink Real-Time object variable, |tg|, load and start the
% model, and modify model parameters.
% 
tg = slrealtime;
tg.load(mdl);
tg.setparam('','soc_dist_level',1);
tg.setparam('','thresh_int_level',1);
tg.start;
while ~strcmp(tg.status,'stopped')
    pause(5);
end    
tg.stop;
%% Run the Model to evaluate the effect of social distancing during the outbreak. 
% 
% Using the Simulink Real-Time object variable, |tg|, load and start the
% model, and modify model parameters
% 
tg = slrealtime;
tg.load(mdl);
tg.setparam('','soc_dist_level',0.2);
tg.setparam('','thresh_int_level',0.2);
tg.start;
while ~strcmp(tg.status,'stopped')
    pause(5);
end    
tg.stop;
%% Display the signals in the Simulation Data Inspector
% To view the plotted signal data, open the Simulation Data Inspector.
% 
Simulink.sdi.view
%% Retrieve and plot signal data from the Simulation Data Inspector
% You can also retrieve the signal data from the SDI and plot the data 
% by using these commands.
% 
% * Get all the runs
% * Get the run information
% * Get the signal.
% * Get the signal objects.
% * Take only infectious group values.
% * Plot the signals.
%
% The result shows that social distancing can reduce the number of
% hospitalized people
%
runIds = Simulink.sdi.getAllRunIDs();

for i = 1:length(runIds)
    run = Simulink.sdi.getRun(runIds(i));
    signalID = run.getSignalIDsByName('hospitalized');
    if ~isempty(signalID) 
        signalObj = Simulink.sdi.getSignal(signalID);
        signalArray(:,i) = signalObj.Values(:,1).Data;
        timeValues = 100*(signalObj.Values(:,1).Time);
        plot(timeValues,signalArray);
        drawnow;
    end 
end 

grid on;
xlabel('Time in days'); ylabel('hospitalized people');

%%
% 
% Copyright 2020 The MathWorks, Inc.
