Documentation Center

  • Trial Software
  • Product Updates

Contents

Simulink.sdi.compareRuns

Compare signal data between two simulation runs

Syntax

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

Description

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

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.

Input Arguments

runID1

Run ID, a unique number identifying the first run for comparison.

runID2

Run ID, a unique number identifying the second run for comparison.

alignmentMethods

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 takes the following values.

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

Output Arguments

diff

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

Examples

Compare two simulation runs using the specified alignment algorithms. The function Simulink.sdi.compareRuns returns a Simulink.sdi.DiffRunResult object containing the comparison results.

% 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
 

Compare normal mode simulation with code generation results for model slexAircraftExample.

% 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

See Also

|

Was this topic helpful?