Compare the data in two simulation runs


diffResult = Simulink.sdi.compareRuns(runID1, runID2)
diffResult = Simulink.sdi.compareRuns(runID1,runID2,alignmentMethods)



diffResult = Simulink.sdi.compareRuns(runID1, runID2) compares the data in the runs corresponding to runID1 and runID2, returning the result in the Simulink.sdi.DiffRunResult object, diffResult.


diffResult = Simulink.sdi.compareRuns(runID1,runID2,alignmentMethods) compares the simulation runs corresponding to runID1 and runID2 using the specified alignment criteria and returns the comparison result in the Simulink.sdi.DiffRunResult object, diffResult. For more information about how the Simulation Data Inspector aligns signals for comparison, see How the Simulation Data Inspector Compares Data.


collapse all

This example shows how to compare runs of simulation data and access the comparison results with the Simulation Data Inspector API.

Generate Runs of Simulation Data

Simulate the model to create runs of simulation data to analyze with the Simulation Data Inspector API.

% Open model

% Set the desired slipratio to 0.24 and simulate
set_param('ex_sldemo_absbrake/Desired relative slip','Value','0.24')

% Change the desired slip ratio to 0.25 and simulate
set_param('ex_sldemo_absbrake/Desired relative slip','Value','0.25')

Compare Runs

Get the run IDs for the runs you just created with the Simulink.sdi.getAllRunIDs function.

% Get run IDs for last two runs
runIDs = Simulink.sdi.getAllRunIDs;
runID1 = runIDs(end - 1);
runID2 = runIDs(end);

% Compare runs
runResult = Simulink.sdi.compareRuns(runID1, runID2);

Create a Plot of a Comparison Result

Use the Simulink.sdi.DiffRunResult object you created in the previous step with Simulink.sdi.compareRuns to access the data for the Ww signal result to plot it in a figure.

% Plot the |Ww| signal difference
signalResult_Ww = runResult.getResultByIndex(1);

You can change tolerance values on a signal-by-signal basis to evaluate the effect of a model parameter change. This example uses the slexAircraftExample model and the Simulation Data Inspector to evaluate the effect of changing the time constant for the low-pass filter following the control input.

% Load example model

% Mark the alpha, rad signal for streaming
Simulink.sdi.markSignalForStreaming('slexAircraftExample/Aircraft Dynamics Model', 4, 'on')

% Simulate system

% Change input filter time constant
modelWorkspace = get_param('slexAircraftExample', 'modelworkspace');
modelWorkspace.assignin('Ts', 0.2)

% Simulate again

% Get run data
runIDs = Simulink.sdi.getAllRunIDs;
runID1 = runIDs(end - 1);
runID2 = runIDs(end);

% Compare runs
diffRun1 = Simulink.sdi.compareRuns(runID1, runID2);

% Get signal result
sigResult1 = diffRun1.getResultByIndex(1);

% Check whether signals matched
ans =



% Get signal object for sigID1
run1 = Simulink.sdi.getRun(runID1);
sigID1 = run1.getSignalIDByIndex(1);
sig1 = Simulink.sdi.getSignal(sigID1);

% Change absolute tolerance to 0.2
sig1.absTol = 0.2;

% Run the comparison again
diffRun2 = Simulink.sdi.compareRuns(runID1, runID2);
sigResult2 = diffRun2.getResultByIndex(1);

% Check the result
ans =



This example shows how to compare runs using your desired criteria for aligning signals between runs.

Generate Runs to Compare

This example simulates the slexAircraftExample model with two different values of Ts to generate two simulation runs for comparison.

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

% Create a run from the simulation output
runID1 = Simulink.sdi.createRun('My Run', 'namevalue', {'simOut'}, ...

% Get workspace and change workspace variable Ts
modelWorkspace = get_param('slexAircraftExample', 'modelworkspace');
modelWorkspace.assignin('Ts', 0.2);

% Simulate again
simOut = sim('slexAircraftExample', 'SaveOutput', 'on', 'SaveFormat', ...
                'Dataset', 'ReturnWorkspaceOutputs', 'on');

% Create another run and get signal IDs
runID2 = Simulink.sdi.createRun('New Run', 'namevalue', {'simOut'}, ...

Define Alignment Criteria for the Comparison

Before running the comparison, define how you want the Simulation Data Inspector to align the signals between the runs. This example aligns signals by their name, then by their block path, and then by their Simulink identifier.

% Define the alignment criteria for the comparison to align signals by
% name, then by block path, then by SID
alignMethods = [Simulink.sdi.AlignType.SignalName

Compare the Runs with the Specified Alignment Criteria

Compare the simulation data in your two runs, with the alignment criteria you specified.

% Compare the runs
diffResults = Simulink.sdi.compareRuns(runID1 ,runID2, alignMethods);

Check the Comparison Results for the Aligned Signals

You can use the getResultByIndex method to access the aligned signals and the results of the comparison for each signal in the runs you compared.

% Check the number of comparisons in the result
numComparisons = diffResults.count;

% Iterate through each element and display results in command window
for k = 1:numComparisons

    resultAtIdx = diffResults.getResultByIndex(k);

    % Get signal IDs for each comparison result
    sigID1 = resultAtIdx.signalID1;
    sigID2 = resultAtIdx.signalID2;

    sig1 = Simulink.sdi.getSignal(sigID1);
    sig2 = Simulink.sdi.getSignal(sigID2);

    % Display whether signals match
    displayStr = 'Signals with IDs %s and %s %s \n';
    if resultAtIdx.match
       fprintf(displayStr, sig1.Name, sig2.Name, 'match');
       fprintf(displayStr, sig1.Name, sig2.Name, 'do not match');

Signals with IDs Actuator Model and Actuator Model do not match 
Signals with IDs Integrate qdot and Integrate qdot do not match 
Signals with IDs Integrate and Integrate do not match 
Signals with IDs Alpha-sensor Low-pass Filter and Alpha-sensor Low-pass Filter do not match 
Signals with IDs Pitch Rate Lead Filter and Pitch Rate Lead Filter do not match 
Signals with IDs Proportional plus integral compensator and Proportional plus integral compensator do not match 
Signals with IDs Stick Prefilter and Stick Prefilter do not match 
Signals with IDs Q-gust model and Q-gust model do not match 
Signals with IDs W-gust model[1] and W-gust model[1] do not match 
Signals with IDs W-gust model[2] and W-gust model[2] do not match 
Signals with IDs alpha, rad and alpha, rad do not match 

Input Arguments

collapse all

Numerical identification for the Baseline run in the comparison.

Numerical identification for the Compare to run in the comparison.

Array specifying alignment algorithms to use to pair signals with the runs for comparison. The Simulation Data Inspector aligns signals first by the first element in the array, then by the second element in the array, and so on.

ValueAligns By
Simulink.sdi.AlignType.BlockPathPath to the signal's source block
Simulink.sdi.AlignType.SIDSimulink® identifier Locate Diagram Components Using Simulink Identifiers
Simulink.sdi.AlignType.SignalNameSignal name
Simulink.sdi.AlignType.DataSourcePath of the variable in the MATLAB® workspace

Example: [Simulink.sdi.AlignType.SignalName, Simulink.sdi.AlignType.SID] specifies signal alignment by name and then by SID.

Output Arguments

collapse all

Simulink.sdi.DiffRunResult object that provides access to comparison results.

Introduced in R2011b

Was this topic helpful?