Compare data in two
compares the signals that correspond to the signal IDs
diff = Simulink.sdi.compareSignals(
sigID2 and returns the results in a
object. For more information on how the comparison results are computed, see How the Simulation Data Inspector Compares Data.
You can use the Simulation Data Inspector programmatic interface to compare signals within a single run. This example compares the input and output signals of an aircraft longitudinal controller.
First, load the session that contains the data.
Simulink.sdi.Run.getLatest function to access the latest run in the data.
aircraftRun = Simulink.sdi.Run.getLatest;
Then, you can use the
Simulink.sdi.getSignalsByName function to access the
Stick signal, which represents the input to the controller, and the
alpha, rad signal that represents the output.
stick = getSignalsByName(aircraftRun,'Stick'); alpha = getSignalsByName(aircraftRun,'alpha, rad');
Before you compare the signals, you can specify a tolerance value to use for the comparison. Comparisons use tolerance values specified for the baseline signal in the comparison, so set an absolute tolerance value of
0.1 on the
stick.AbsTol = 0.1;
Now, compare the signals using the
Simulink.sdi.compareSignals function. The
Stick signal is the baseline, and the
alpha, rad signal is the signal to compare against the baseline.
comparisonResults = Simulink.sdi.compareSignals(stick.ID,alpha.ID); match = comparisonResults.Status
match = OutOfTolerance
The comparison result is out of tolerance. You can use the
Simulink.sdi.view function to open the Simulation Data Inspector to view and analyze the comparison results.
This example shows how to compare signals from different simulation runs using the Simulation Data Inspector's
Simulink.sdi.compareSignals function. When you only have one signal of interest to compare, using a signal comparison returns the
Simulink.sdi.diffSignalResult object with the comparison data directly.
Generate Simulation Data
slexAircraftExample model to generate simulation runs. Between the runs, change the time constant of the input filter.
% Load example model load_system('slexAircraftExample') % Mark the alpha, rad signal for streaming Simulink.sdi.markSignalForStreaming('slexAircraftExample/Aircraft Dynamics Model',4,'on') % Simulate system out_1 = sim('slexAircraftExample'); % Change input filter time constant modelWorkspace = get_param('slexAircraftExample','modelworkspace'); assignin(modelWorkspace,'Ts',0.2) % Simulate again out_2 = sim('slexAircraftExample');
Get Signal IDs for the Signal Comparison
Create run objects using the run IDs, and then use
getSignalIDByIndex to get the signal IDs to pass to
% Get run data runIDs = Simulink.sdi.getAllRunIDs; runID1 = runIDs(end-1); runID2 = runIDs(end); run1 = Simulink.sdi.getRun(runID1); run2 = Simulink.sdi.getRun(runID2); sigID1 = getSignalIDByIndex(run1,1); sigID2 = getSignalIDByIndex(run2,1);
Compare the signals, and open the Simulation Data Inspector to view the results.
diffResult = Simulink.sdi.compareSignals(sigID1,sigID2); Simulink.sdi.view
sigID1— Signal ID of baseline signal
sigID2— Signal ID of signal to compare
diff— Signal comparison results
Simulink.sdi.diffSignalResult| array of
Signal comparison results, returned as a
Simulink.sdi.DiffSignalResult object. Complex signal
comparison results are returned as an array of two
DiffSignalResult objects. One
DiffSignalResult object contains the real data and the
other contains the imaginary data. Check the
property of the
DiffSignalResult object to determine
whether it contains real or imaginary data.
The Simulation Data Inspector does not support comparing:
Signals of data types