%% Compare Runs with Alignment Criteria
% When you compare runs using the Simulation Data Inspector, you can specify alignment criteria that determine how signals are paired with each other for comparison. This example compares data from simulations of a model of an aircraft longitudinal control system. The simulations used a square wave input. The first simulation used an input filter time constant of 0.1s and the second simulation used an input filter time constant of 0.5s.
% First, load the simulation data from the session file that contains the data for this example.

Simulink.sdi.load('AircraftExample.mldatx');

%
% The session file contains data for four simulations. This example compares data from the first two runs. Access the run IDs for the first two runs loaded from the session file.

runIDs = Simulink.sdi.getAllRunIDs;
runIDTs1 = runIDs(end-3);
runIDTs2 = runIDs(end-2);

%
% 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.

alignMethods = [Simulink.sdi.AlignType.SignalName
               Simulink.sdi.AlignType.BlockPath
               Simulink.sdi.AlignType.SID];

%
% 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.

diffResults = Simulink.sdi.compareRuns(runIDTs1,runIDTs2,'align',alignMethods,...
    'timetol',0.005);

%
% You can use the getResultByIndex function to access the comparison results for the aligned signals in the runs you compared. You can use the Count property of the Simulink.sdi.DiffRunResult object to set up a for loop to check the Status property for each Simulink.sdi.DiffSignalResult object.

numComparisons = diffResults.count;

for k = 1:numComparisons
    resultAtIdx = getResultByIndex(diffResults,k);
    
    sigID1 = resultAtIdx.signalID1;
    sigID2 = resultAtIdx.signalID2;
    
    sig1 = Simulink.sdi.getSignal(sigID1);
    sig2 = Simulink.sdi.getSignal(sigID2);
    
    displayStr = 'Signals %s and %s: %s \n';
    fprintf(displayStr,sig1.Name,sig2.Name,resultAtIdx.Status);
end

% Copyright 2018 The MathWorks, Inc.
%