Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 36 ยท Stateflow

Stateflow Transitions, Actions, Data, Events and Functions

Master Stateflow transition labels: conditions, condition and transition actions, execution order, junctions, local/input/output data, events and chart functions.

The first chart lesson showed states and simple transitions. Real charts need precise rules: which of several valid transitions wins, when each kind of action runs, what data belongs to the chart, and how to avoid repeating logic. This lesson covers those rules. As before, the product cannot run here, so Python stand-ins show the behavior and are labelled as such.

The parts of a transition label

TEXT
event [condition] {condition_action} / transition_action
  • event (optional): the transition is checked only when this event occurs. Without an event it is checked at every chart wake-up.
  • [condition]: a Boolean expression. The transition is valid only when it is true, for example [speed > 80 && !fault].
  • {condition_action}: runs as soon as the condition is true, before the source state is left. Often used to compute something the destination needs or to mark a check.
  • / transition_action: runs after the source state is left and before the destination is entered, if the whole path is valid.

The order for a taken transition from A to B is therefore: condition true, condition action, exit A, transition action, enter B. Because a condition action runs even if a later part of a multi-segment path turns out to be invalid, keep important side effects in transition actions or entry actions.

When several transitions are valid

If a state has two outgoing transitions and both are valid, the chart needs a rule. Classic charts use the 12 o'clock rule by default: the transitions are tested clockwise starting from the top of the state, so their on-screen position decides. This is fragile, so set Chart Properties > User specified state/transition execution order and then the Execution order appears as a number near each transition. Explicit order is easier to review and independent of how you drew the diagram.

Practical guidance: write conditions so that at most one can be true, and add an explicit order for the rest.

Junctions

A connective junction is a small circle that connects segments of transitions, so that you can share part of a path or build if/else structures.

flowchart LR
    A["Idle"] -->|"[start]"| J(("junction"))
    J -->|"[mode == 1]"| B["Slow"]
    J -->|"[mode == 2]"| C["Fast"]
    J -->|"1 (default branch)"| D["Error"]

One shared segment from Idle, then branches. The last branch has a condition that is always true and is tested last.

Rules for junctions:

  • A path through junctions is taken only if the whole path to a state is valid; otherwise Stateflow backtracks and tries the next outgoing transition.
  • Put an else branch (an unconditioned transition with the lowest execution order) last, so the chart never gets stuck.
  • A junction can loop back to itself to make a for loop or a while loop using condition actions (rarely needed; a function is clearer).

Data and scope

Data is defined in the Symbols pane or the Model Explorer. The scope says who can see it:

Scope Meaning
Local Belongs to the chart, keeps its value between time steps (like a state variable).
Input Receives a Simulink signal through an input port.
Output Writes a Simulink signal through an output port.
Constant A fixed value for the chart.
Parameter A value tuned from the base workspace or mask, can change between runs but not during one.
Data Store Memory A variable shared with the rest of the model through a Data Store Memory block.
Temporary Exists only inside a function call.

Every datum has a type (double, single, int8..uint32, boolean, a fixed-point type, an enumeration, a bus object, or Inherit: Same as Simulink), a size and optional min/max limits. Setting minimum and maximum on inputs and outputs helps later with fixed-point scaling and with the checks in the testing chapter.

Give local data an initial value; otherwise it starts at zero. Remember that local data persists while the chart is active, but the chart resets it when the chart is disabled and re-enabled if the enclosing subsystem has the reset behavior.

Events

An event is a named trigger. You declare it in the Symbols pane (Add Event > Input Event, Local Event or Output Event) and use it in transition labels. A local event can be broadcast from an action with send(E) (MATLAB) or just E; in C action language. Input events come from a trigger port or a function-call signal.

Events give you precise control: a transition labeled COIN [credit >= 100] can happen only when the COIN event occurs. Events can also be used to wake a chart rarely, for example only when a button is pressed, which saves simulation time.

In many models you do not need events at all: a chart that runs at a fixed sample time and checks conditions on its inputs is simple, deterministic and easy to code-generate. Use events when you want an explicit signal that something just happened.

Functions inside a chart

Repeating logic is a smell. Stateflow offers four kinds of function:

  • Graphical function: a flow chart with junctions, drawn in the chart. Good for decision trees.
  • MATLAB function: written in MATLAB text inside the chart (right-click > Add Function > MATLAB Function, or use the toolbar).
  • Truth table function: a decision table (see temporal logic and truth tables).
  • Simulink function: a block diagram that the chart calls like a function, to reuse existing Simulink logic.

A graphical or MATLAB function is called by name from an action, for example limit = clampSpeed(rawSpeed);. Functions that need no outputs are called like statements.

A worked example you can run as a stand-in

A door controller has states Closed, Opening, Open and Closing. We code the transitions with an explicit order, the way you would set it in the editor, and use a condition action to count cycles.

Python Stand-in: transitions with condition action, transition action and order

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.

The "else" branch of Opening mirrors a junction whose last branch is unconditioned. Moving the blocked test to order 1 matters: if the else were first, the blocked case would never be reached.

Reuse with an action table

A second stand-in shows how to keep the data about states separate from the logic. In real charts you would use a State Transition Table for this.

Python Stand-in: a transition table drives the chart

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.

Common mistakes

  • Putting essential work in a condition action on a path that may be rejected by a later junction.
  • Relying on drawing position for the order of transitions.
  • Forgetting the unconditional else branch, so the chart waits forever in a state.
  • Data type mismatch between a chart input (for example boolean) and the Simulink signal; set Inherit: Same as Simulink or convert explicitly.
  • Changing local data in two parallel states at once, which makes results depend on the order (see hierarchy and parallel states).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In what order do the parts of a taken transition run?

    The condition action runs once the condition is true, then the source state is exited, the transition action runs, and the destination is entered.
  2. 2. Why set explicit transition execution order instead of relying on the 12 o'clock rule?

    Position-based order breaks when someone moves a transition; explicit numbers are stable and clear.
  3. 3. Which data scope keeps its value between time steps inside the chart?

    Local data belongs to the chart and persists; temporary data lives only during a function call.
  4. 4. What is the purpose of a last, unconditioned branch from a junction?

    Without a fallback, a chart can stop in a state when none of the conditions is true.

Summary

  • A transition label is event [condition] {condition_action} / transition_action; the order is condition, condition action, exit, transition action, entry.
  • Use explicit execution order and an else branch to remove ambiguity.
  • Junctions share path segments and build decision trees; a path is taken only if it reaches a state.
  • Data has a scope (local, input, output, constant, parameter, data store, temporary), a type and optional limits.
  • Events are optional triggers; functions (graphical, MATLAB, truth table, Simulink) remove repeated logic.

Next: Hierarchy, parallel states and history