This example shows how to use the Simulink.sdi.constraints.MatchesSignal
constraint to compare time series data using data generated in the workspace. You can use the MatchesSignal
constraint to compare data from many sources, including measured test data in a file and logged simulation outputs.
When you use the MatchesSignal
constraint, you can specify absolute, relative, and time tolerances to use in the comparison. The MatchesSignal
constraint uses the Simulation Data Inspector comparison algorithm, which includes steps for alignment and synchronization. For more information about the algorithm, see How the Simulation Data Inspector Compares Data.
The MatchesSignal
constraint uses the Simulation Data Inspector for the time series comparison and requires one of these products:
Create Time Series Data
This example generates workspace data in timeseries
format for illustrative purposes only. You can use the MatchesSignal
constraint to compare data from other sources, such as simulation outputs or a file containing test data, and the constraint can compare time series data in any format supported by the Simulation Data Inspector.
In this example, the first signal is a timeseries
object containing data for a sine wave with the name Wave Data
.
The second signal is also a sine wave at the same frequency, with a slight attenuation and sampled at a different rate. Because the comparison algorithm for the MatchesSignal
constraint includes a synchronization step, comparing these two signals with different time vectors does not always result in a test failure. The second signal is also named Wave Data
so the alignment algorithm can pair the signals for comparison.
Create a Test Case and Compare the Signals
This example uses an interactive test for illustrative purposes. You can use the MatchesSignal
constraint with other types of unit tests in the MATLAB testing framework. For example, to write class-based unit tests, see Write Simple Test Case Using Classes.
Create a TestCase
instance for interactive use.
Compare sig1_ts
and sig2_ts
using the MatchesSignal
constraint. Use sig1_ts
for the expected value in the instance of the MatchesSignal
constraint.
Verification failed.
---------------------
Framework Diagnostic:
---------------------
MatchesSignal(<Value>) failed.
--> The following aligned signals did not match:
Name ActualSignals ExpectedSignals
___________ _______________ _________________
"Wave Data" "<Actual>.Data" "<Expected>.Data"
Comparison results have been saved to:
--> /tmp/Bdoc20b_1498024_127381/comparisonResults_bc39a908-d8e2-41ea-a615-c8d3b0d09ba6.mldatx
------------------
Stack Information:
------------------
In /tmp/Bdoc20b_1498024_127381/tpb0048a0f/simulink-ex22803619/TestTimeSeriesDataWithTimeToleranceExample.mlx (TestTimeSeriesDataWithTimeToleranceExample) at 14
In /mathworks/devel/bat/Bdoc20b/build/matlab/toolbox/matlab/codetools/embeddedoutputs/+matlab/+internal/+editor/evaluateRegions.p (evaluateRegions) at 0
In /mathworks/devel/bat/Bdoc20b/build/matlab/toolbox/matlab/codetools/embeddedoutputs/+matlab/+internal/+editor/EvaluationOutputsService.p (EvaluationOutputsService.evalRegions) at 0
In /mathworks/devel/bat/Bdoc20b/build/matlab/toolbox/matlab/codetools/+matlab/+internal/+liveeditor/@LiveEditorUtilities/execute.m (execute) at 43
In /mathworks/devel/bat/Bdoc20b/build/matlab/tools/examples/exampletools/+mwtools/liveCodeToDocbook.m (doRun) at 364
In /mathworks/devel/bat/Bdoc20b/build/matlab/tools/examples/exampletools/+mwtools/liveCodeToDocbook.m (doRunConvert) at 314
In /mathworks/devel/bat/Bdoc20b/build/matlab/tools/examples/exampletools/+mwtools/liveCodeToDocbook.m (liveCodeToDocbook) at 143
In /mathworks/devel/bat/Bdoc20b/build/matlab/tools/build_using_matlab/BML.m (BML) at 13
The diagnostic information shows that the verification failed. When a test fails, the comparison results and original run data are saved in a Simulation Data Inspector session file. Click the link for the MLDATX file to open the session in the Simulation Data Inspector, where you can investigate the data compared by the test and the comparison results. When you inspect the results, you can see the attenuation creates the difference and causes the comparison to fail.
Specify an absolute tolerance of 0.02
for the instance of the MatchesSignal
constraint to use in the comparison.