Execute and Unit TestStateflowChart Objects
A standalone Stateflow®chart is a MATLAB®class that defines the behavior of a finite state machine. Standalone charts implement classic chart semantics with MATLAB as the action language. You can program the chart by using the full functionality of MATLAB, including those functions that are restricted for code generation in Simulink®。For more information, seeCreate Stateflow Charts for Execution as MATLAB Objects。
Example of a Standalone Stateflow Chart
The filesf_chart.sfx
contains the standalone Stateflow chartsf_chart
。The chart has local datau
,x
, andy
。
This example shows how to execute this chart from the Stateflow Editor and in the MATLAB Command Window.
Execute a Standalone Chart from theStateflowEditor
To unit test and debug a standalone chart, you can execute the chart directly from the Stateflow Editor. During execution, you enter data values and broadcast events from the user interface.
Open the chart in the Stateflow Editor:
editsf_chart.sfx
In theSymbolspane, enter a value of
u
= 1and clickRun。The chart executes its default transition and:Initializes
x
to the value of 0.Makes state
A
the active state.Assigns
y
to the value of 1.Increases the value of
x
to 1.
The chart animation highlights the active state
A
。TheSymbolspane displays the valuesu
= 1,x
= 1, andy
= 1。The chart maintains its current state and local data until the next execution command.ClickStep。Because the value of
u
does not satisfy the condition[u<0]
to transition out of stateA
, this state remains active and the values ofx
andy
increase to 2. TheSymbolspane now displays the valuesu
= 1,x
= 2, andy
= 2。In theSymbolspane, enter a value of
u
= −1and clickStep。消极的数据值触发转换to stateB
。TheSymbolspane displays the valuesu
= −1,x
= 1, andy
= 3。You can modify the value of any chart data in theSymbolspane. For example, enter a value of
x
= 3。The chart will use this data value in the next time execution step.Enter a value of
u
= 2and clickStep。The chart transitions back to stateA
。TheSymbolspane displays the valuesu
= 2,x
= 4, andy
= 5。To stop the chart animation, clickStop。
To interrupt the execution and step through each action in the chart, add breakpoints before you execute the chart. For more information, seeDebug a Standalone Stateflow Chart。
Execute a Standalone Chart inMATLAB
You can execute a standalone chart in MATLAB without opening the Stateflow Editor. If the chart is open, then the editor highlights active states and transitions through chart animation.
Open the chart in the Stateflow Editor. In the MATLAB Command Window, enter:
editsf_chart.sfx
Create the Stateflow chart object by using the name of the
sfx
file for the standalone chart as a function. Specify the initial value for the datau
as a name-value pair.s = sf_chart(u=1)
Stateflow ChartExecution Function y = step(s) Local Data u : 1 x : 1 y : 1 Active States: {'A'}
s
, executes the default transition, and initializes the values ofx
andy
。The Stateflow Editor animates the chart and highlights the active stateA
。To execute the chart, call the
step
function. For example, suppose that you call thestep
function with a value ofu
= 1:step(s,u=1)
disp(s)
Stateflow ChartExecution Function y = step(s) Local Data u : 1 x : 2 y : 2 Active States: {'A'}
u
does not satisfy the condition[u<0]
to transition out of stateA
, this state remains active and the values ofx
andy
increase to 2.Execute the chart again, this time with a value of
u
= −1:step(s,u=-1)
disp(s)
Stateflow ChartExecution Function y = step(s) Local Data u : -1 x : 1 y : 3 Active States: {'B'}
B
。The value ofx
decreases to 1 and the value ofy
increases to 3.To access the value of any chart data, use dot notation. For example, you can assign a value of 3 to the local data
x
by entering:s.x = 3
Stateflow ChartExecution Function y = step(s) Local Data u : -1 x : 3 y : 3 Active States: {'B'}
Execute the chart with a value of
u
= 2:step(s,u=2)
disp(s)
Stateflow ChartExecution Function y = step(s) Local Data u : 2 x : 4 y : 5 Active States: {'A'}
A
and modifies the values ofx
andy
。To stop the chart animation, delete the Stateflow chart object
s
:delete(s)
Execute Multiple Chart Objects
You can execute multiple chart objects defined by the same standalone chart. Concurrent chart objects maintain their internal state independently, but remain associated to the same chart in the editor. The chart animation reflects the state of the chart object most recently executed. Executing multiple chart objects while the Stateflow Editor is open can produce confusing results and is not recommended.