Documentation Center

  • Trial Software
  • Product Updates

Contents

Record and Inspect Signal Data Programmatically

Overview

Using the Simulation Data Inspector API, you can plot signal data, compare two signals, and compare two simulation runs of data. 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:

To...Use...
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.

To...Use...
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 recorded simulation

Simulink.sdi.setRunOverwrite

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.

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

Simulink.sdi.getSignal

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

Simulink.sdi.Signal class

Import/Export Data

To...Use...
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

Comparison Results

To...Use...
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 API, the Simulation Data Inspector does not automatically record simulation data. To create a run of simulation data, you can use the following code:

% Open the model 'slexAircraftExample'
load_system('slexAircraftExample');

% Configure model "slexAircraftExample" for logging and simulate
simOut = sim('slexAircraftExample', 'SaveOutput','on', ...
             'SaveFormat', 'StructureWithTime', ...
             'ReturnWorkspaceOutputs', 'on');
% Create a Simulation Data Inspector run
[runID,runIndex,signalIDs] = Simulink.sdi.createRun('My Run',...
     'namevalue',{'MyData'},{simOut});

Compare Signal Data

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

% Configure model "slexAircraftExample" for logging and simulate
simOut = sim('slexAircraftExample', 'SaveOutput','on', ...
             'SaveFormat', 'StructureWithTime', ...
             'ReturnWorkspaceOutputs','on');

% Create a Simulation Data Inspector run and get signal IDs
[~, ~, signalIDs] = Simulink.sdi.createRun('My Run', ...
      'namevalue', {'MyData'}, {simOut});

sig1 = signalIDs(1);
sig2 = signalIDs(2);

% Compare two signals, which returns the results in an instance 
% of Simulink.sdi.diffSignalResult
diff = Simulink.sdi.compareSignals(sig1, sig2);

% Find if the signal data match
match = diff.match;
 
% Get the tolerance used in Simulink.sdi.compareSignals
tolerance = diff.tol;

Compare Runs of Simulation Data

To compare the signal data between two simulation runs, you can use the following code:

% Configure model "slexAircraftExample" for logging and simulate
set_param('slexAircraftExample/Pilot','WaveForm','square');
simOut = sim('slexAircraftExample', 'SaveOutput','on', ...
                  'SaveFormat', 'StructureWithTime', ...
                  'ReturnWorkspaceOutputs', 'on');

% Create a Simulation Data Inspector run, Simulink.sdi.Run, from 
% simOut in the base workspace
runID1 = Simulink.sdi.createRun('First Run','namevalue', ...
    {'simOut'},{simOut});

% Simulate again
set_param('slexAircraftExample/Pilot','WaveForm','sawtooth');
simOut = sim('slexAircraftExample', 'SaveOutput','on', ...
             'SaveFormat', 'StructureWithTime', ...
             'ReturnWorkspaceOutputs', 'on');
      
% Create another Simulation Data Inspector run
runID2 = Simulink.sdi.createRun('Second Run','namevalue', ...
   {'simOut'},{simOut});

% Compare two runs, the result is stored in a 
% Simulink.sdi.DiffRunResult object
difference = Simulink.sdi.compareRuns(runID1, runID2);
      
% Number of comparisons in result
numComparisons = difference.count;
      
% Iterate through each result element
for i = 1:numComparisons
   % Get signal result at index i
   signalResult = difference.getResultByIndex(i);
 
   % Get signal IDs for each comparison result
   sig1 = signalResult.signalID1;
   sig2 = signalResult.signalID2;
 
   % Display if signals match or not
   displayStr = 'Signals with IDs %d and %d %s \n';
   if signalResult.match
      fprintf(displayStr, sig1, sig2, 'match');
   else
      fprintf(displayStr, sig1, sig2, 'do not match');
   end
end

Specify Signal Tolerances

To control relative and absolute signal tolerances, you can use the following code:

% Configure model "slexAircraftExample" for logging and simulate
simOut = sim('slexAircraftExample', 'SaveOutput','on', ...
               'SaveFormat', 'StructureWithTime', ...
               'ReturnWorkspaceOutputs', 'on');
 
% Create a Simulation Data Inspector run
[runID,runIndex,signalIDs] = Simulink.sdi.createRun('My Run', ...
				'base',{'simOut'});
 
% Get the Simulink.sdi.Run object corresponding to the new run ID
runObj = Simulink.sdi.getRun(runID);
 
% Get the number of signals in the run
numSignals = runObj.signalCount;

% Get the Simulink.sdi.Signal objects for each signal in the run
% Specify the absolute and relative tolerance for each signal
for i = 1:numSignals
   signalObjs(i) = runObj.getSignal(signalIDs(i));
   signalObjs(i).absTol = 0.5;
   signalObjs(i).relTol = 0.005;
end

Record Data During Parallel Simulations

This example shows how to run multiple simulations in a parfor loop and record each run in the Simulation Data Inspector. Use the Simulink.sdi.getSource, Simulink.sdi.setSource, and Simulink.sdi.refresh methods to get and set the location of the Simulation Data Inspector repository. To record data during parallel simulations, you can use the following code:

Open the Simulation Data Inspector.

Simulink.sdi.view;

Load the model.

mdl = 'slexAircraftExample';
load_system(mdl);

Get the location of the simulation data repository.

src = Simulink.sdi.getSource();

Start a parallel pool with 4 workers.

parpool(4);

Run the simulation in a parfor loop.

parfor i=1:4
   % Set the location of the simulation data repository of this 
   % worker to be the same for aggregating the data
   Simulink.sdi.setSource(src);
   % Run the simulation
   simOut = sim(mdl,'SaveOutput','on',...
                'SaveFormat','StructureWithTime',...
                'ReturnWorkspaceOutputs','on');
   % Create a simulation run in the Simulation Data Inspector
   Simulink.sdi.createRun(['Run' num2str(i)],'namevalue',...
                          {'simout'},{simOut});
end

Delete the current parallel pool and close all of the models.

parpool close;
bdclose all;

Refresh the Simulation Data Inspector.

Simulink.sdi.refresh();

See Also

| | |

Was this topic helpful?