In this example, you evaluate a Simulink® model, detect unexpected behavior, and use Model Slicer to programmatically isolate and resolve the unexpected behavior. When you plan to reuse your API commands and extend their use to other models, a programmatic approach is useful.
Be familiar with the behavior and purpose of Model Slicer and the functionality of
the Model Slicer API. Highlight Functional Dependencies outlines how to use Model Slicer user
interface to explore models. The
slslicertrace function reference
pages contain the Model Slicer API command help.
sldvSliceCruiseControlHarness test harness model contains a
cruise controller subsystem
sldvSliceCruiseControl and a block,
TestCases, containing a test case for this subsystem. You
first simulate the model to execute the test case. You then evaluate the behavior of
the model to find and isolate areas of the model responsible for unexpected
sldvSliceCruiseControlHarness test harness for
the cruise control
The Assertion block is set to Stop simulation when assertion fails when the actual operation mode is not the same as the expected operation mode.
The TestCases block contains several test inputs for sldvSliceCruiseControl.
In the TestCases Signal Builder click the Run all button to run all of the included test cases. You receive an error during the ResumeWO test case.
The Assertion block halted simulation at 27 seconds, because the actual operation mode was not the same as the expected operation mode. Click OK to close this error message.
In the sldvSliceCruiseControlHarness model, double-click the Assertion block, clear Enable assertion, and click OK.
Set the Active Group of the
TestCases Signal Builder block to the test case
containing the error and run the simulation
signalbuilder('sldvSliceCruiseControlHarness/TestCases', 'ACTIVEGROUP', 12) sim('sldvSliceCruiseControlHarness')
The Scope block in the model contains three signals:
operation_mode – displays the actual operation
mode of the subsystem.
expected_mode – displays the expected operation
mode of the subsystem that the test case provides.
verify – displays a Boolean value comparing the
operation mode and the expected mode.
The scope shows a disparity between the expected operation mode and the
actual operation mode beginning at time
27. Now that you
know the outport displaying the unexpected behavior and the time window
containing the unexpected behavior, use Model Slicer to isolate and analyze
the unexpected behavior.
Create a Model Slicer configuration object for the model using
slslicer. The Command
Window displays the slice properties for this Model Slicer
obj = slslicer('sldvSliceCruiseControlHarness')
obj = SLSlicer with properties: Configuration: [1x1 SLSlicerAPI.SLSlicerConfig] ActiveConfig: 1 DisplayedConfig:  StorageOptions: [1x1 struct] AnalysisOptions: [1x1 struct] SliceOptions: [1x1 struct] InlineOptions: [1x1 struct] Contents of active configuration: Name: 'untitled' Description: '' Color: [0 1 1] SignalPropagation: 'upstream' StartingPoint: [1x0 struct] ExclusionPoint: [1x0 struct] Constraint: [1x0 struct] SliceComponent: [1x0 struct] UseTimeWindow: 0 CoverageFile: '' UseDeadLogic: 0 DeadLogicFile: ''
Activate the slice highlighting mode of Model Slicer to compile the model and prepare it for dependency analysis.
operation_mode outport block as a starting
point and highlight it.
The area of the model upstream of the starting point and active during simulation is highlighted.
Simulate the model within a restricted simulation time window (maximum 30 seconds) to highlight only the area of the model upstream of the starting point and active during the time window of interest.
You can further narrow the simulation time window by changing the start time to 20 seconds.
Create a sliced model
sldvSliceCruiseControlHarness_sliced containing only
the area of
slicedModel = slice(obj,'sldvSliceCruiseControlHarness_sliced') open_system('sldvSliceCruiseControlHarness_sliced')
The sliced model
contains a simplified version of the source model
sldvSliceCruiseControlHarness. The simplified standalone
model contains only those parts of the model that are upstream of the specified
starting point and active during the time window of interest.
You can now debug the unexpected behavior in the simplified standalone model and then apply changes to the source model.
To enable editing the model again, terminate the Model Slicer mode.
Navigate to the area of the sliced model that contains the unexpected behavior.
AND Logical Operator block in this subsystem has a
true constant attached to its second input
true constant indicates that the second input
port is always
true during the restricted time window for this sliced
model, causing the cruise control system not to enter the "has canceled"
Navigate to the equivalent
AND Logical Operator block
in the source system by using
slslicertrace to view the
blocks connected to the second input port.
h = slslicertrace('SOURCE',... 'sldvSliceCruiseControlHarness_sliced/Model/CruiseControlMode/opMode/resumeCondition/hasCanceled/LogicOp1') hilite_system(h)
OR Logical Operator block in this subsystem is always
true in the current configuration. Changing the
OR Logical Operator block to an
AND Logical Operator block rectifies this
Before making edits, create new copies of the cruise control model and the test harness model.
Update the model reference in the test harness to refer to the newly saved model.
Use the block path of the erroneous Logical Operator block to fix the error.
Simulate the test harness for 45 seconds with the fixed model to confirm the corrected behavior.
ans = Simulink.SimulationOutput: tout: [4501x1 double] SimulationMetadata: [1x1 Simulink.SimulationMetadata] ErrorMessage: [0x0 char]
The scope now shows that the expected operation mode is the same as the actual operation mode.
To complete the demo, save and close all models and remove the Model Slicer configuration object.
save_system('sldvSliceCruiseControl_fixed') save_system('sldvSliceCruiseControlHarness_fixed') close_system('sldvSliceCruiseControl_fixed') close_system('sldvSliceCruiseControlHarness_fixed') close_system('sldvSliceCruiseControlHarness_sliced') clear obj