Main Content

Use State Transition Tables to Express Sequential Logic in Tabular Form

The State Transition Table block represents a finite state machine for sequential modal logic in tabular format. Instead of drawing states and transitions in a Stateflow® chart, you can use a state transition table to model a state machine in a concise, compact format that requires minimal maintenance of graphical objects.

In a state transition table, rows represent the states in your system. The transition columns specify the condition, condition action, and destination state for each outgoing transition from a state.

Diagram of state transition table highlighting state column, transition column, condition cell, condition action cell, and destination state cell.

For example, this state transition table contains the modal logic for maintaining the temperature of a boiler between two set points, reference_low and reference_high. During normal operation, the boiler cycles between the states Off, Warmup, and On.

State transition table with two top-level states called Normal and Alarm. Normal has three substates called Off, Warmup, and On.

The state transition table represents the same modal logic as this Stateflow chart.

Stateflow chart with two top-level states called Normal and Alarm. Normal has three substates called Off, Warmup, and On.

For more information about this example, see Model Bang-Bang Controller by Using a State Transition Table.

Program a State Transition Table

To create a State Transition Table:

  1. Create a Simulink® model that contains a State Transition Table block by calling the function sfnew.

    sfnew -STT

  2. Double-click the State Transition Table block.

    Block icon for state transition table.

  3. For each operating mode in your system, add a state row and enter a state label, as described in Define Actions in a State. To organize complex systems, define a hierarchy of states by adding child state rows below a parent state row.

    • To add a state row, select an existing state and, in the Modeling tab, choose from one of these options:

      • Insert State Row — Add a state at the same level of hierarchy.

      • Insert Child State Row — Add a state as a child of the selected state.

    • To move a state row, click the top edge of the state and drag the state to a new location. As you drag the state, the editor displays a graphical cue that indicates the new position of the state.

    • To model operating modes that are active at the same time, enable parallel (AND) decomposition in a parent state. For more information, see Simulate Parallel States with a State Transition Table.

  4. To represent the direction of flow logic between states, specify conditions, condition actions, and destinations for the outgoing transitions from each state.

    • To add a transition column, right-click the header for an existing column and choose from one of these options:

      • Append transition column — Add a transition column to the right of the table.

      • Insert transition column — Add a transition column to the left of the selected column.

    • To move the condition, action, and destination cells for a transition, click the top edge of the condition cell and drag the transition left or right. The condition, action, and destination cells move together as a single unit.

    • To specify the destination of the transition, in the destination state cell, select the name of a state or one of these options:

      • $NEXT — Create a transition to the next sibling state. This option is not available for the last substate in each level of the state hierarchy.

      • $PREV — Create a transition to the previous sibling state. This option is not available for the first substate in each level of the state hierarchy.

      • $SELF — Create a self-loop transition.

    • To comment out a transition, in the destination state cell, select % IGNORE %.

  5. At each level of the hierarchy, mark the first state to become active when the parent becomes active. Select a state and, in the Modeling tab, select Set as Default State.

    Alternatively, to specify a default transition path with multiple branching points, in the Modeling tab, select Insert Default Transition Row. For example, this default transition row selects between two destinations, A1 and A2, depending on the value of condition.

    Default transition row with transitions to states A1 and A2.

  6. To specify an inner transition from the a parent state to one or more child states, in the Modeling tab, select Insert Inner Transition Row. For example, in this inner transition row, the input event E triggers an inner transition that selects between two destinations, B1 and B2, depending on the values of condition1 and condition2.

    Inner transition row with transitions to child states B1 and B2.

    You must specify destination states in an inner transition row in the same order that the corresponding child states appear in the table. For instance, in the previous example, state B1 must appear above state B2.

  7. If your system has inputs or outputs, or depends on any state variables, add input, output, and local data as described in Add Stateflow Data.

  8. If your system reacts to event triggers or must trigger actions in your chart or other blocks in your model, add input and output events, as described in Synchronize Model Components by Broadcasting Events.

  9. Connect the State Transition Table block to other blocks in the Simulink model by using input and output ports.

  10. To simulate the model, click Run. During the simulation, the state transition table highlights the active states and transitions.

Simulate Parallel States with a State Transition Table

In Stateflow, the substates of states that use parallel decomposition are active simultaneously. For example, if State A uses parallel decomposition and has two substates, A1 and A2, both A1 and A2 are active at the same time. For more information on parallel states, see Define Exclusive and Parallel Modes by Using State Decomposition.

To use parallel states in State Transition Table blocks, enter the state transition table. To change the decomposition at the chart level, enter the state transition table, but do not select a state. In the Modeling tab, select Decomposition > Parallel (AND). To give a state parallel decomposition, select the state whose decomposition you want to change. In the Modeling tab, select Decomposition > Parallel (AND).

Note

Before updating the decomposition of your chart or state to Parallel (AND), all cells in the transition column of the state must be clear.

Parallel states have a dashed outline that indicates that they are active simultaneously.

State transition table that shows parallel state decomposition.

Detect Errors in State Transition Tables

To run diagnostic checks on a state transition table, in the Debug tab, select Update Model > Update Table. The diagnostics tool statically parses the table to find errors such as:

  • Unresolved symbols

  • Unreachable states

  • Default transition rows without an unconditional transition

  • Transition cells with conditions or actions, but no destination

  • Action text in a condition cell

  • Inner transition rows that specify destination states in a different order than the corresponding states appear in the table

These error checks are also performed during simulation. For more information about debugging state transition tables, see Debug Run-Time Errors in a State Transition Table.

Specify Properties for State Transition Tables

State transition table properties specify how your state transition table interfaces with the Simulink model. You can modify these properties in the Property Inspector, the Model Explorer, or the State Transition Table properties dialog box.

To use the Property Inspector:

  1. Open the State Transition Table block.

  2. In the Modeling tab, under Design Data, select Property Inspector.

  3. In the Property Inspector, edit the state transition table properties.

To use the Model Explorer:

  1. In the Modeling tab, under Design Data, select Model Explorer.

  2. In the Model Hierarchy pane, select the state transition table.

  3. In the Dialog pane, edit the state transition table properties.

To use the State Transition Table properties dialog box:

  1. Open the State Transition Table block.

  2. In the Modeling tab, click Table Properties.

  3. In the properties dialog box, edit the state transition table properties.

You can also modify state transition table properties programmatically by using Stateflow.StateTransitionTableChart objects. For more information about the Stateflow programmatic interface, see Overview of the Stateflow API.

Tip

State transition table properties are a subset of the properties for Stateflow charts. For a description of each property, see Specify Properties for Stateflow Charts.

Guidelines for Using State Transition Tables

  • State transition tables can use MATLAB® or C as the action language. For more information, see Differences Between MATLAB and C as Action Language Syntax.

  • State transition tables must have at least one state row and one transition column.

  • State transition tables do not support these elements of Stateflow charts:

    • Supertransitions

    • Local events

    • Chart-level graphical, truth table, MATLAB, and Simulink functions

See Also

Blocks

Functions

Objects

Related Topics