%% 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 |slrt_ex_file_logging|. This model generates 20 sinusoids, 
% each having a different amplitude.
%
% *Note:* Scopes of type 'target' are limited to 10 signals.
%
% Open the model.
% 
mdlOpen = 0;
systems = find_system('type', 'block_diagram');
if all(~strcmp('slrt_ex_file_logging', systems))
    mdlOpen = 1;
    open_system(fullfile(matlabroot,'toolbox','slrealtime','examples','slrt_ex_file_logging'));
end
%%
% Build the model and download to the target computer:
%
% * Configure for a non-Verbose build.
% * Build and download application.
%
set_param('slrt_ex_file_logging','RTWVerbose','off');
rtwbuild('slrt_ex_file_logging');
%%
% * Close the model if it is open.
%
if (mdlOpen)
    bdclose('slrt_ex_file_logging');
end
%% Run the Model
% 
% Using the Simulink Real-Time object variable, |tg|, start the model.
% 
tg = slrealtime('TargetPC1');
load(tg,'slrt_ex_file_logging');
start(tg);
pause(5);
my_status = status(tg);
while strcmp(my_status, 'running')
    pause(0.05);
    my_status = status(tg);
end
%% Display the signals in the Simulation Data Inspector
% To view the plotted signal data, open the Simulation Data Inspector.
% 
Simulink.sdi.view
%%
% <<../simulation_data_inspector_sine_waves.png>>
% 
%% 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
%
runIds = Simulink.sdi.getAllRunIDs(); 
run = Simulink.sdi.getRun(runIds(end)); 
signals=run.SignalCount;
array=1;
%%
% * Get the signal.
% * Get the signal objects.
% * Take only Sine values.
%
for signalIndex=1:signals
    signalID = run.getSignalIDByIndex(signalIndex);   
    signalObj = Simulink.sdi.getSignal(signalID); 
    if(~isempty(strfind(signalObj.Name,'Sine'))) 
        signalArray(:,array)=signalObj.Values(:,1).Data;
        timeValues=signalObj.Values(:,1).Time;
        array=array+1;
    end
end
%%
% * Plot the signals.
% 
%  plot(timeValues,signalArray);
%  grid on;
%  xlabel('Time (sec)'); ylabel('Amplitude');
%
%% More Information
% 
% * <docid:simulink_ug.bt313u0-1>
% 
% Copyright 2020 The MathWorks, Inc.