Documentation

Inspect and Compare Signal Data Programmatically

Overview

Using the Simulation Data Inspector API, you can plot signal data, compare two signals, and compare data from two simulation runs. You can use the Simulink.sdi.createRun function to add simulation output data to the Simulation Data Inspector. Once the Simulation Data Inspector contains signal data, you can perform the following tasks:

GoalUse
View signal data, open the Simulation Data Inspector

Simulink.sdi.view

Compare the data of two signals

Simulink.sdi.compareSignals

Compare the output of two simulation runs

Simulink.sdi.compareRuns

Run Management

Simulation Data Inspector software creates and manages a list of simulation runs. Each run is an instance of a Simulink.sdi.Run object. This object contains all of the simulation data and metadata for that simulation run.

GoalUse
Get the number of runs currently in the Simulation Data Inspector

Simulink.sdi.getRunCount

Get the run ID from the list of simulation runs in the Simulation Data Inspector

Simulink.sdi.getRunIDByIndex

Determine if a run ID corresponds to a run currently in the Simulation Data Inspector

Simulink.sdi.isValidRunID

Add more data to a run currently in the Simulation Data Inspector

Simulink.sdi.addToRun

Make a copy of a run currently in the Simulation Data Inspector

Simulink.sdi.copyRun

Delete a run from the Simulation Data Inspector

Simulink.sdi.deleteRun

Get simulation data for a run in the Simulation Data Inspector

Simulink.sdi.getRun

Specify a run naming rule using tokens and regular characters as a template

Simulink.sdi.setRunNamingRule

Mark a run in the Simulation Data Inspector for overwriting on the next simulation

Simulink.sdi.setRunOverwrite

Change signal grouping hierarchy of Runs pane

Simulink.sdi.setTableGrouping

Manage output signal data and metadata of a simulation run

Simulink.sdi.Run class

Get the Simulink.sdi.Signal object corresponding to the given signal ID

Simulink.sdi.Run.getSignal method

Get the Simulink.sdi.Signal object corresponding to the index into the array signals in the run

Simulink.sdi.Run.getSignalByIndex method

Get the signal ID corresponding to the index into the array signals in the run

Simulink.sdi.Run.getSignalIDByIndex method

Determine if a signal ID corresponds to a signal currently in the run

Simulink.sdi.Run.isValidSignalID method

Signal Management

Each Simulink.sdi.Run object contains a Simulink.sdi.Signal object for each output signal data. This object contains all of the simulation data for the signal and its metadata.

GoalUse
Get the simulation data and metadata for a signal from one simulation run.

Simulink.sdi.getSignal

Copy signal line style and color from one run to another

Simulink.sdi.copyRunViewSettings

Set number of subplots

Simulink.sdi.setSubPlotLayout

Delete a signal from the Simulation Data Inspector

Simulink.sdi.deleteSignal

Manages a signal's time series data and metadata for one simulation run.

Simulink.sdi.Signal class

Turn streaming on or off for a signal

Simulink.sdi.markSignalForStreaming

Change signals marked for logging to streaming

Simulink.sdi.changeLoggedToStreamed

Change signals marked for streaming to logging

Simulink.sdi.changeStreamedToLogged

Import/Export Data

GoalUse
Save signal data currently in the Simulation Data Inspector

Simulink.sdi.save

Load previously saved Simulation Data Inspector session

Simulink.sdi.load

Clear all data from the Simulation Data Inspector

Simulink.sdi.clear

Close the Simulation Data Inspector and save data

Simulink.sdi.close

Generate report from Simulation Data Inspector

Simulink.sdi.report

Create run from simulation output results

Simulink.sdi.createRunsFromSimulationOutput

Import Simulink® Design Verifier™ test case inputs

Simulink.sdi.createRunsFromSLDV

Comparison Results

GoalUse
Manage the results of comparing two runs (Simulink.sdi.compareRuns creates the Simulink.sdi.DiffRunResult object)

Simulink.sdi.DiffRunResult class

Manage the results of comparing two signals (Simulink.sdi.compareSignals creates the Simulink.sdi.DiffSignalResult object)

Simulink.sdi.DiffSignalResult class

Create a Run in the Simulation Data Inspector

To populate the Simulation Data Inspector with runs of simulation data, you must first simulate your model and then call Simulink.sdi.createRun. When using the sim function, logged workspace data is not automatically sent to the Simulation Data Inspector. To create a run of simulation data, you can use the following code:

% Open model and stream logged signals
% to Simulation Data Inspector
load_system('sldemo_absbrake');
Simulink.sdi.changeLoggedToStreamed('sldemo_absbrake');

% Simulate model
sim('sldemo_absbrake');

% Open Simulation Data Inspector
Simulink.sdi.view;

Create a Run from Workspace Variables

To create a run in the Simulation Data Inspector from variables in the base workspace, you can use the following code:

% Create timeseries data
t = (0:0.1:2*pi)';
ts1 = timeseries(sin(t),t,'Name','sine');
ts2 = timeseries(cos(t),t,'Name','cosine');

% Import timeseries into Simulation Data Inspector
runID = Simulink.sdi.createRun('My Run','vars',ts1,ts2);
run = Simulink.sdi.getRun(runID);

% Plot signals
sig1 = getSignalByIndex(run,1);
sig2 = getSignalByIndex(run,2);

sig1.checked = true;
sig2.checked = true;

% Open Simulation Data Inspector
Simulink.sdi.view

Stream Data and Send to Base Workspace

If you stream data into the Simulation Data Inspector, then you can send it to the base workspace using the following code:

% Open model
load_system('sldemo_absbrake');

% Streamed logged signals directly to
% Simulation Data Inspector and automatically
% export them at end of simulation
Simulink.sdi.changeLoggedToStreamed('sldemo_absbrake');
set_param('sldemo_absbrake','StreamToWorkspace','on');

% Simulate model and stream data to base workspace
out = sim('sldemo_absbrake','ReturnWorkspaceOutputs','on');

Compare Signal Data

To compare the simulation data for two signals, you can use the following code:

% Open model and stream logged signals
% to Simulation Data Inspector
load_system('sldemo_absbrake');
Simulink.sdi.changeLoggedToStreamed('sldemo_absbrake');

% Simulate model
sim('sldemo_absbrake');

% Create a Simulation Data Inspector run
runID = Simulink.sdi.getRunIDByIndex(1);
run = Simulink.sdi.getRun(runID);

sig1 = getSignalIDByIndex(run,1);
sig2 = getSignalIDByIndex(run,2);

% Compare two signals
diff = Simulink.sdi.compareSignals(sig1,sig2);

% Find if signal data match
Match = diff.Match;

% Get tolerance used when comparing signals
tolerance = diff.Tol;

Compare Runs of Simulation Data

To compare the signal data between two simulation runs, you can use the following code. At the end of this example, the difference between the Ww signal from each run is plotted in a MATLAB® figure.

% Open model and stream logged signals
% to Simulation Data Inspector
load_system('sldemo_absbrake');
Simulink.sdi.changeLoggedToStreamed('sldemo_absbrake');

% Simulate model using 0.24 desired slip
set_param('sldemo_absbrake/Desired relative slip','Value','0.24')
sim('sldemo_absbrake');

% Simulate model using 0.25 desired slip
set_param('sldemo_absbrake/Desired relative slip','Value','0.25')
sim('sldemo_absbrake');

% Compare runs
run1 = Simulink.sdi.getRunIDByIndex(1);
run2 = Simulink.sdi.getRunIDByIndex(2);
runResult = Simulink.sdi.compareRuns(run1,run2);

% Plot 'Ww' signal difference between runs
signalResult = getResultByIndex(runResult,1);
plot(signalResult.Diff);

Generate a Report from Simulation Data Inspector

To create a report from a run comparison, you can use the following code:

% Open model and stream logged signals
% to Simulation Data Inspector
load_system('sldemo_absbrake');
Simulink.sdi.changeLoggedToStreamed('sldemo_absbrake');

% Simulate model twice
set_param('sldemo_absbrake/Desired relative slip','Value','0.22')
sim('sldemo_absbrake');

set_param('sldemo_absbrake/Desired relative slip','Value','0.25')
sim('sldemo_absbrake');

% Compare two runs
runID1 = Simulink.sdi.getRunIDByIndex(1);
runID2 = Simulink.sdi.getRunIDByIndex(2);
Simulink.sdi.compareRuns(runID1,runID2);

% Create report
Simulink.sdi.report('ReportToCreate','Compare Runs');

Record Data During Parallel Simulations

This example shows how to run parallel simulations using a parfor loop and record each run in the Simulation Data Inspector.

% Open Simulation Data Inspector
Simulink.sdi.view;

% Load model
mdl = 'sldemo_mdlref_bus';
load_system(mdl);

% Start parallel pool with four workers
myPool = parpool(4);

% Run simulation in parfor loop
out = cell(1,4);
parfor idx = 1:4
   % Run simulation
   out{idx} = sim(mdl,'ReturnWorkspaceOutputs','on');

   % Create a simulation run in
   % Simulation Data Inspector
   runName = sprintf('Run %d',idx);
   Simulink.sdi.createRun(runName,'vars',out{idx});
end

% Delete current parallel
% pool and close model
delete(myPool);
close_system(mdl);

% Import data from parallel pool
% into Simulation Data Inspector
Simulink.sdi.importDataFromWorkers;

See Also

Was this topic helpful?