Documentation

Simulink.sdi.compareRuns

Compare signal data between two simulation runs

Syntax

  • diff = Simulink.sdi.compareRuns(runID1,runID2)
    example
  • diff = Simulink.sdi.compareRuns(runID1,runID2,alignmentMethods)
    example

Description

example

diff = Simulink.sdi.compareRuns(runID1,runID2) compares the matched signals between two simulation runs and returns their differences in a Simulink.sdi.DiffRunResult object.

example

diff = Simulink.sdi.compareRuns(runID1,runID2,alignmentMethods) compares the matched signals between two simulation runs using specified alignment algorithms and returns their differences in a Simulink.sdi.DiffRunResult object.

Examples

collapse all

Compare Simulation With Code Generation Results

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

% Configure model "slexAircraftExample" for logging
set_param('slexAircraftExample','SolverType','Fixed-Step','SaveOutput','on',...
          'SaveFormat','StructureWithTime','ReturnWorkspaceOutputs','on');

% CD to temporary directory and build   
cd(tempdir);
rtwbuild('slexAircraftExample');

% Run the executable 
if ispc
   system('slexAircraftExample');
elseif unix
   system('./slexAircraftExample');
end

% Create a run using the slexAircraftExample.mat placed in the current directory
[run1ID,~,~] = Simulink.sdi.createRun('My Run','file','slexAircraftExample.mat');

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

% Create another run from the simulation
[run2ID,~,~] = Simulink.sdi.createRun('My Run','namevalue',...
                                      {'MyData'},{simOut});

% Compare the two runs
difference = Simulink.sdi.compareRuns(run1ID,run2ID);

% Number of comparisons in result
numComparisons = difference.count;

% Iterate through each result element
for i = 1:numComparisons
   % Get result at index i
   resultAtIdx = difference.getResultByIndex(i);
  
   % Get signal IDs for each comparison result
   sig1 = resultAtIdx.signalID1;
   sig2 = resultAtIdx.signalID2;
  
   % Display if signals match or not
   displayStr = 'Signals with IDs %d and %d %s \n';
   if resultAtIdx.match
      fprintf(displayStr,sig1,sig2,'match.');
   else
      fprintf(displayStr,sig1,sig2,'do not match.');
end

% Plot tolerance and difference results in a figure
f1 = figure;
plot(resultAtIdx.tol,'Color','r'); 
hold on;
plot(resultAtIdx.diff,'Color','g'); 
legend('Tolerance','Difference');
end

Compare Two Runs Using Specified Alignment Algorithms

% Configure model "slexAircraftExample" for logging and simulate
simOut = sim('slexAircraftExample','SaveOutput','on',...
              'SaveFormat','StructureWithTime',...
              'ReturnWorkspaceOutputs','on');
 
% Create a run and get signal IDs
run1ID = Simulink.sdi.createRun('My Run','namevalue',...
                                 {'simOut'},{simOut});
 
% Get and change one of the parameters of the model
mws = get_param('slexAircraftExample','modelworkspace');
wsMq = mws.evalin('Mq');
mws.assignin('Mq',3*wsMq);
 
% Simulate again
simOut = sim('slexAircraftExample','SaveOutput','on',...
              'SaveFormat','StructureWithTime',...
              'ReturnWorkspaceOutputs','on');
      
% Create another run and get signal IDs
run2ID = Simulink.sdi.createRun('New Run','namevalue',...
                                {'simOut'},{simOut});
 
% Define the alignment algorithms for comparison.
% Align the data first by data name, then by block path, then by SID.
algorithms = [Simulink.sdi.AlignType.DataSource
               Simulink.sdi.AlignType.BlockPath
               Simulink.sdi.AlignType.SID];
 
% Compare the two runs
difference = Simulink.sdi.compareRuns(run1ID,run2ID,algorithms);
      
% Number of comparisons in result
numComparisons = difference.count;
      
% Iterate through each result element
for i = 1:numComparisons
    % Get result at index i
    resultAtIdx = difference.getResultByIndex(i);
 
    % Get signal IDs for each comparison result
    sig1 = resultAtIdx.signalID1;
    sig2 = resultAtIdx.signalID2;
 
    % Display if signals match or not
    displayStr = 'Signals with IDs %d and %d %s \n';
    if resultAtIdx.match
       fprintf(displayStr,sig1,sig2,'match');
    else
       fprintf(displayStr,sig1,sig2,'do not match');
    end
 
    % Plot tolerance and difference results in a figure
    f1 = figure;
    plot(resultAtIdx.tol,'Color','r'); 
    hold on;
    plot(resultAtIdx.diff,'Color','g'); 
    legend('Tolerance','Difference');
end

Related Examples

Input Arguments

collapse all

runID1 — Unique run identifierinteger

Run ID, a unique number identifying the first run for comparison, specified as an integer.

runID2 — Unique run identifierinteger

Run ID, a unique number identifying the second run for comparison, specified as an integer.

alignmentMethods — Signal alignment methodsarray

An array specifying three alignment algorithms. Data is aligned by the value of the first element of the array, then by the second element, and then by the third element. Only the first three values in the array are considered. The array can use the following values.

ValueAlign By
Simulink.sdi.AlignType.BlockPathPath to the source block for the signal
Simulink.sdi.AlignType.DataSourceData name (for example, logsout.Stick.Data)
Simulink.sdi.AlignType.SIDSimulink Identifier
Simulink.sdi.AlignType.SignalNameSignal name

For example, [Simulink.sdi.AlignType.DataSource,Simulink.sdi.AlignType.BlockPath,Simulink.sdi.AlignType.SID] could be an alignment array.

Output Arguments

collapse all

diff — Comparison difference dataobject

Instance of the Simulink.sdi.DiffRunResult object that contains the differences between two simulation runs.

Introduced in R2011b

Was this topic helpful?