Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 36 ยท Stateflow

Stateflow Charts: States, Transitions and Your First Chart

Learn what Stateflow adds to Simulink: add a Chart block, draw states and transitions, add entry and during actions, set the chart's update method and watch it animate.

Block diagrams are excellent for equations and signal flow, but they are awkward for decisions such as "if the door is open and the motor is running, stop, then wait five seconds, then restart". Logic with modes, conditions and sequences is better drawn as a state machine. Stateflow is the Simulink add-on that gives you state machines inside a model: you draw states and transitions in a Chart block, and the chart runs together with the rest of the model.

Simulink and Stateflow are commercial MathWorks products and cannot run on this site. We describe the menus and the exact behavior, and use small Python stand-ins, clearly labelled, that implement the same ideas so you can experiment with the rules. Nothing here shows output from the real product.

Where Stateflow fits

A Simulink model is a set of blocks that compute outputs from inputs at every time step. A Stateflow chart is one such block, but inside it you see a diagram of states (modes the system can be in) and transitions (arrows that say when it leaves one mode for another). Typical uses are supervisory logic, mode management (start-up, run, fault, shutdown), protocols and sequences.

flowchart LR
    S["Simulink: continuous and discrete signals"] --> C["Stateflow Chart block<br/>modes and decisions"]
    C --> S2["Simulink: commands to the plant"]

The chart reads input signals, decides what mode you are in and writes output signals.

Add a chart to a model

  1. Open or create a model (Simulink > Blank Model).
  2. Open the Library Browser, find Stateflow, and drag a Chart block into the model. You can also double-click an empty canvas and type chart.
  3. Double-click the block to open the Stateflow Editor. The editor has its own toolbar (state, junction, history junction, function and data tools) and a Symbols pane that lists the chart's data.
  4. Add input and output data: in the Symbols pane choose Add Data > Input Data and give it a name (for example temp). Inputs appear as ports on the chart block and are fed by Simulink signals.

You can also create a chart from the command line with sfnew, or start from a State Transition Table or Truth Table block, which describe the same logic in a table form (covered in a later lesson).

In current releases the editor opens in a tab inside the model window, and chart types are Classic (graphical), State Transition Table and Truth Table. Menu names move a little between releases; the concepts below do not.

Draw states and transitions

  • State: click the State tool in the left toolbar, then click on the canvas. Type the name in the top-left of the box, for example Idle.
  • Transition: hover over the edge of a state; the pointer becomes a cross. Drag from there to another state. Drag from the edge of one state to the edge of the same state to create a self-loop.
  • Default transition: an arrow with a dot that comes from nowhere. It tells the chart which state is entered the first time. Click the Default Transition tool and drag onto the state. A chart without it makes Simulink report an error when it updates.
  • Transition label: click the transition and type its label. The general form is event [condition] {condition_action} / transition_action, and each part is optional. For example [temp > 30] means the transition can be taken only while temp is above 30.
stateDiagram-v2
    [*] --> Off
    Off --> On: [switchIn == 1]
    On --> Off: [switchIn == 0]

The classic two-state chart. The black dot is the default transition, so the chart starts in Off.

State actions

Text inside a state, below its name, describes what happens at different moments of its life:

Keyword Short form When it runs
entry: en: Once, when the state becomes active
during: du: Every time step the state stays active and no outgoing transition is taken
exit: ex: Once, when the state stops being active

For example, a state Heating can contain:

TEXT
Heating
entry: heater = 1;
during: counter = counter + 1;
exit: heater = 0;

You can also write several keywords on one line, en, du: lamp = 1;. The language is the chart's action language, MATLAB by default or C if you choose it in the chart properties. Use MATLAB for models that mix with other MATLAB code, C when you want the generated code to look like C.

The execution rule in one sentence

At each time step an active chart does this: if there is a default transition and no state is active, take it; otherwise, check the outgoing transitions of the active state in order, and if one is valid, leave the state (run exit), run the transition actions and enter the destination (run entry). If no transition is valid, run the state's during actions. The chart stops after one such "reaction" per time step.

Here is a runnable stand-in for that rule. It is our own tiny interpreter, not Stateflow, but it follows the entry, during and exit order.

Python Stand-in: a two-state chart with entry, during and exit actions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that the first call only takes the default transition: at that step the chart enters Off and does not look at the input yet. This matches the real behavior: the first time step enters the default state.

Chart properties that matter

Right-click an empty part of the chart and choose Properties (or Chart > Properties in the editor):

  • Update method: Inherited (the chart runs when its inputs update), Discrete (with a Sample time, for example 0.01) or Continuous (the chart runs at every solver minor step, only when you really need it). For logic, choose Discrete or Inherited; this decides how often "one time step" happens.
  • Action language: MATLAB or C.
  • Enable C-bit operations, Initialize outputs every time chart wakes up and similar options, which you can leave at their defaults for now.

If your chart is inside a fixed-step model with step 0.01 s, a state with during: n = n + 1; increments n 100 times per second of simulated time.

Simulate and watch it animate

Press Run in the Simulation tab. In the chart the active state is highlighted and transitions flash as they are taken. For slow viewing, enable Simulation > Stateflow Animation (in the debug options), then use the Step Forward button in the Debug tab to move one time step at a time. Put a breakpoint on a state or transition (right-click > Set Breakpoint) and the simulation pauses there so you can inspect data in the Symbols pane.

To watch the active state as a signal, open the properties of the chart (or of a parent state), tick Create output for monitoring with Child activity, and send the resulting output port to a Scope. The value is a number that identifies which child is active.

Common mistakes

  • No default transition. The chart does not know where to start; Simulink reports an error.
  • Two transitions valid at once without an order. The chart picks one using a fixed rule (explained in the next lesson), which may surprise you. Make conditions mutually exclusive or order them explicitly.
  • Forgetting that the condition is checked every time step. A transition [temp > 30] fires at the first step where it is true, then you are in a new state.
  • Using the chart for continuous math. Use blocks for that and keep the chart for decisions.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a default transition (an arrow with a dot) define?

    The default transition tells Stateflow which state to enter at the start. Without it the chart cannot start.
  2. 2. When does a state's during action run?

    Entry runs on entering, exit on leaving, and during on each step spent in the state without leaving it.
  3. 3. Where do you add an input signal that comes from Simulink to a chart?

    Input data appears as an input port on the Chart block, and you connect a Simulink signal to it.
  4. 4. What is the general form of a transition label?

    All four parts are optional and appear in that order.

Summary

  • Stateflow adds state machines to Simulink through the Chart block; use it for modes, sequences and decisions, and use ordinary blocks for equations.
  • Draw states and transitions, always give the chart a default transition and label transitions as event [condition] {condition_action} / transition_action.
  • entry, during and exit actions run at entering, staying in and leaving a state.
  • The chart's update method decides how often it reacts; the simulation animates the active state and supports breakpoints.
  • The Python code above is a stand-in; the real product is not available on this site.

Next: Stateflow transitions, actions and data