Simulink.sdi.compareRuns

Compare the data in two simulation runs

Description

example

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.

example

diffResult = Simulink.sdi.compareRuns(runID1,runID2,Name,Value) compares the simulation runs corresponding to runID1 and runID2 using the options specified by one or more Name,Value pair arguments 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.

Examples

collapse all

This example shows how to compare runs of simulation data and analyze the comparison results with the Simulation Data Inspector programmatic interface.

Generate Runs of Simulation Data

Simulate the model with different Desired relative slip values to create runs of simulation data to analyze with the Simulation Data Inspector programmatic interface.

% Open model
load_system('ex_sldemo_absbrake')

% Set the desired slip ratio to 0.24 and simulate
set_param('ex_sldemo_absbrake/Desired relative slip','Value','0.24')
out_1 = sim('ex_sldemo_absbrake');

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

Compare Runs with a Global Tolerance

Get the run IDs for the runs you just created with the Simulink.sdi.getAllRunIDs function. Then, compare the runs using a global relative tolerance and a global time tolerance to analyze whether your data meets specifications.

% 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,'reltol',0.2,'timetol',0.5);

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 = getResultByIndex(runResult,1);
figure(1)
plot(signalResult_Ww.Diff)

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.

Setup

Load the model, and mark the q, rad/sec and alpha, rad signals for logging. Then, simulate the model to create the baseline run.

% Load example model
load_system('slexAircraftExample')

% Mark the q, rad/sec and alpha, rad signals for logging
Simulink.sdi.markSignalForStreaming('slexAircraftExample/Aircraft Dynamics Model',3,'on')
Simulink.sdi.markSignalForStreaming('slexAircraftExample/Aircraft Dynamics Model',4,'on')

% Simulate system
out_1 = sim('slexAircraftExample');

Modify Model Parameter

Modify the model parameter Ts in the model workspace to change the time constant of the input low-pass filter.

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

% Simulate again
out_2 = sim('slexAircraftExample');

Compare Runs and Inspect Results

Use the Simulink.sdi.compareRuns function to compare the data from the simulations. Then, inspect the match property of the signal result to see whether the signals fell within the default tolerance of 0.

% 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
sig1Result1 = getResultByIndex(diffRun1,1);
sig2Result1 = getResultByIndex(diffRun1,2);

% Check whether signals matched
sig1Result1.Status
ans = 
OutOfTolerance
sig2Result1.Status
ans = 
OutOfTolerance

Compare Runs with Signal Tolerances

The signals did not match within the default tolerance of 0. To further analyze the effect of the time constant change, add signal tolerances to the comparison with the baseline signal properties to determine the tolerance required for a pass. This example uses a combination of time and absolute tolerances.

% Get signal object for sigID1
run1 = Simulink.sdi.getRun(runID1);
sigID1 = getSignalIDByIndex(run1,1);
sigID2 = getSignalIDByIndex(run1,2);

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

% Set tolerances for q, rad/sec
sig1.AbsTol = 0.1;
sig1.TimeTol = 0.6;

% Set tolerances for alpha, rad
sig2.AbsTol = 0.2;
sig2.TimeTol = 0.8;

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

% Check the result
sig1Result2.Status
ans = 
WithinTolerance
sig2Result2.Status
ans = 
WithinTolerance

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

Generate Simulation Data to Compare

Start by loading the slexAircraftExample system and marking signals of interest for logging.

% Load the model slexAircraftExample
load_system('slexAircraftExample')

% Mark signals for logging
Simulink.sdi.markSignalForStreaming('slexAircraftExample/Pilot',1,'on')
Simulink.sdi.markSignalForStreaming('slexAircraftExample/Aircraft Dynamics Model',... 
    4,'on')

This example runs two simulations of the slexAircraftExample model with different values of the variable Ts. Use get_param to access the model workspace for the slexAircraftExample model. Then, assign a value for the variable and run the first simulation.

% Get the model workspace and set the value of variable Ts
modelWorkspace = get_param('slexAircraftExample','modelworkspace');
modelWorkspace.assignin('Ts',0.1);

% Configure model "slexAircraftExample" for logging and simulate
out_1 = sim('slexAircraftExample');

Change the value of Ts and run the second simulation. Then, use the Simulation Data Inspector programmatic interface to access the run IDs for the two simulation runs.

% Modify the value of model workspace variable Ts
modelWorkspace.assignin('Ts',0.2);
 
% Simulate again with the new value of Ts
out_2 = sim('slexAircraftExample');
      
% Get run IDs for simulations
runIDs = Simulink.sdi.getAllRunIDs;
runID1 = runIDs(end-1);
runID2 = runIDs(end);

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
               Simulink.sdi.AlignType.BlockPath
               Simulink.sdi.AlignType.SID];

Compare the Runs with the Specified Alignment Criteria

Compare the simulation data in your two runs, using the alignment criteria you specified. The comparison uses a small time tolerance to account for the effect of differences in the step size used by the solver on the transition of the square wave input.

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

Check the Comparison Results for the Aligned Signals

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

% Check the number of comparisons in the result
numComparisons = diffResults.count;
      
% Display result for each signal 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 %s and %s: %s \n';
    fprintf(displayStr,sig1.Name,sig2.Name,resultAtIdx.Status);
end
Signals alpha, rad and alpha, rad: OutOfTolerance 
Signals Stick and Stick: WithinTolerance 

Input Arguments

collapse all

Numerical identification for the Baseline run in the comparison.

Numerical identification for the Compare to run in the comparison.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: 'abstol',x,'align',alignOpts

Array specifying alignment algorithms to use for pairing signals from 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.

Positive-valued global absolute tolerance used for all signals in the run comparison. For more information about tolerances in the Simulation Data Inspector, see How the Simulation Data Inspector Compares Data.

Example: 0.5

Data Types: double

Positive-valued global relative tolerance used for all signals in the run comparison. The relative tolerance is expressed as a fractional multiplier. For example, 0.1 specifies a 10 percent tolerance. For more information about how the relative tolerance is applied in the Simulation Data Inspector, see How the Simulation Data Inspector Compares Data.

Example: 0.1

Data Types: double

Positive-valued global time tolerance used for all signals in the run comparison. Specify the time tolerance with units of seconds. For more information about tolerances in the Simulation Data Inspector, see How the Simulation Data Inspector Compares Data.

Example: 0.2

Data Types: double

Output Arguments

collapse all

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

Introduced in R2011b