Main Content

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

  1. Open the chart in the Stateflow Editor:

    editsf_chart.sfx

  2. In theSymbolspane, enter a value ofu= 1and clickRun。The chart executes its default transition and:

    • Initializesxto the value of 0.

    • Makes stateAthe active state.

    • Assignsyto the value of 1.

    • Increases the value ofxto 1.

    The chart animation highlights the active stateA。TheSymbolspane displays the valuesu= 1,x= 1, andy= 1。The chart maintains its current state and local data until the next execution command.

  3. ClickStep。Because the value ofudoes not satisfy the condition[u<0]to transition out of stateA, this state remains active and the values ofxandyincrease to 2. TheSymbolspane now displays the valuesu= 1,x= 2, andy= 2

  4. In theSymbolspane, enter a value ofu= −1and clickStep。消极的数据值触发转换to stateB。TheSymbolspane displays the valuesu= −1,x= 1, andy= 3

  5. You can modify the value of any chart data in theSymbolspane. For example, enter a value ofx= 3。The chart will use this data value in the next time execution step.

  6. Enter a value ofu= 2and clickStep。The chart transitions back to stateA。TheSymbolspane displays the valuesu= 2,x= 4, andy= 5

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

  1. Open the chart in the Stateflow Editor. In the MATLAB Command Window, enter:

    editsf_chart.sfx

  2. Create the Stateflow chart object by using the name of thesfxfile for the standalone chart as a function. Specify the initial value for the datauas 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'}
    This command creates the chart objects, executes the default transition, and initializes the values ofxandy。The Stateflow Editor animates the chart and highlights the active stateA

  3. To execute the chart, call thestepfunction. For example, suppose that you call thestepfunction 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'}
    Because the value ofudoes not satisfy the condition[u<0]to transition out of stateA, this state remains active and the values ofxandyincrease to 2.

  4. Execute the chart again, this time with a value ofu= −1:

    step(s,u=-1)
    disp(s)
    Stateflow ChartExecution Function y = step(s) Local Data u : -1 x : 1 y : 3 Active States: {'B'}
    消极的数据值触发转换to stateB。The value ofxdecreases to 1 and the value ofyincreases to 3.

  5. To access the value of any chart data, use dot notation. For example, you can assign a value of 3 to the local dataxby entering:

    s.x = 3
    Stateflow ChartExecution Function y = step(s) Local Data u : -1 x : 3 y : 3 Active States: {'B'}
    The standalone chart will use this data value in the next time execution step.

  6. Execute the chart with a value ofu= 2:

    step(s,u=2)
    disp(s)
    Stateflow ChartExecution Function y = step(s) Local Data u : 2 x : 4 y : 5 Active States: {'A'}
    The chart transitions back to stateAand modifies the values ofxandy

  7. To stop the chart animation, delete the Stateflow chart objects:

    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.

See Also

||

Related Topics