Skip to content
Tutorials Center
My learning

Topics

Lesson 21 of 36 ยท Stateflow

Stateflow Hierarchy, Parallel States and History Junctions

Structure large Stateflow charts with superstates, exclusive (OR) and parallel (AND) decomposition, execution order, history junctions and super-transitions.

A flat chart with fifteen states and forty arrows is unreadable. Stateflow gives you three tools for large logic: hierarchy (states inside states), parallelism (several state machines active at once) and history (remember where you were). This lesson explains all three and the order in which they run. Stateflow cannot run on this site; the Python stand-ins implement the same rules and are labelled as such.

Hierarchy: superstates and substates

Drag a state into another state's box (or draw the new state inside it). The outer state is the parent (a superstate), the inner ones are children (substates). A parent can contain a whole state machine.

Benefits:

  • One transition out of the parent applies to every child, so "emergency stop from any substate" is one arrow instead of five.
  • You can collapse a parent to see only the overview and open it for details (right-click > Make Contents > Subcharts turns a state into a subchart that has its own tab).
  • Entry and exit of the parent happen around the children's activity: the parent's entry runs first, then the child's default transition; leaving the parent runs the active child's exit first, then the parent's exit.
stateDiagram-v2
    [*] --> Operating
    state Operating {
        [*] --> Idle
        Idle --> Running: start
        Running --> Idle: stop
    }
    Operating --> Fault: error
    Fault --> Operating: reset

The single arrow error leaves Operating no matter which child is active.

Exclusive (OR) and parallel (AND) decomposition

Each parent has a decomposition that says how its children relate:

  • Exclusive (OR): exactly one child is active at a time. The child boxes have solid borders. This is the default.
  • Parallel (AND): all children are active together. The child boxes have dashed borders and show a small number in the top-right corner: their execution order.

Set it by right-clicking the parent > Decomposition > Exclusive (OR) or Parallel (AND). Parallel states need no default transition because they all become active when the parent is entered.

Typical use: a machine has a mode state machine (Off, Standby, Run) and, at the same time, a fault monitor machine (Healthy, Warning, Alarm). Writing them as one flat machine would need the product of both sets of states (3 x 3 = 9 states); parallel decomposition keeps them separate.

flowchart TB
    P["Machine (parallel decomposition)"] --> A["Mode<br/>Off, Standby, Run"]
    P --> B["Monitor<br/>Healthy, Warning, Alarm"]
    P --> C["Counter<br/>logging"]

All three children are active and react in the order of their numbers.

Execution order

When a chart wakes up, Stateflow processes parallel siblings in order of their numbers, top to bottom and left to right by default. You can change the Execution order in the state's properties or by right-click > Execution Order. The order matters whenever one parallel state writes data that another reads. If Monitor computes alarm and Mode reads it, run Monitor first, or you get a one-step delay.

Inside each active state, the reaction follows this rule (simplified):

  1. Test the state's outgoing transitions in order; if one is valid, take it.
  2. Otherwise run the state's during action.
  3. Then react in the active child (or children, for parallel states).

Because a parent is checked before its children, a transition from the parent has priority over transitions of the children.

A runnable stand-in for hierarchy and parallel states

The stand-in below has a parent Machine with two parallel children, Mode and Monitor, and shows how execution order changes the data a state sees.

Python Stand-in: two parallel state machines and execution 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.

Compare the two runs: when Monitor runs first, Mode sees the new alarm in the same step; when Mode runs first, it reacts one step later. In Stateflow you choose that order explicitly.

History junctions

When you leave a parent state and later re-enter it, the chart normally enters the child pointed to by the default transition. A history junction (a circle with an H) inside the parent changes that: the parent remembers its last active child and re-enters it.

Use history for "pause and resume" behavior: a washing machine that goes to Paused and then resumes where it was, or a menu system that returns to the last page. Draw the H junction inside the parent. The first time the parent is entered there is no history yet, so the parent's ordinary default transition still decides the child.

History applies only to the level where it is placed: it remembers the active child of that parent, not of nested parents (put one in each level that needs it).

Python Stand-in: history remembers the last active child

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.

Super-transitions and transitions across levels

A transition may cross levels: from a child out to a state that belongs to a different parent, or from outside straight into a nested child (a super-transition, which can be labeled to enter a specific substate). Rules:

  • Leaving a nested state runs all exit actions from the innermost outward.
  • Entering a nested state runs entry actions from the outermost inward.
  • Crossing levels makes diagrams hard to read. Prefer a transition to the parent, and let the parent's default transition decide the child, unless a specific child is really needed.

Subcharts and atomic subcharts

A subchart is a state shown as one box with its own canvas, handy when a chart gets big, but it still belongs to the same chart. An atomic subchart is stored as a reusable unit (even in a library) and has restricted access to the rest of the chart's data, which suits modular design and testing. Use Make Contents > Subchart and Convert to Atomic Subchart (right-click on the state).

Common mistakes

  • Parallel states changing the same variable. The result depends on execution order; give each variable one writer.
  • A forgotten default transition in an OR parent (parallel parents do not need one).
  • Using history everywhere. It hides the initial state. Use it only where resuming is intended.
  • Deep nesting. More than three levels is hard to review; use a subchart or a function.
  • Assuming the order is by name. It is by number, which defaults to position.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In a parallel (AND) decomposition how many children are active?

    Parallel children are all active together and react in order of their execution numbers.
  2. 2. What does a history junction do?

    Without it the parent re-enters its default child; with history it resumes where it left off.
  3. 3. A transition leaves a parent state. What happens to the active child?

    Exit actions run from the innermost active state outward.
  4. 4. Why would you reorder two parallel states?

    Order decides whether a reader sees the new value in the same step or one step later.

Summary

  • Hierarchy groups states so that one transition from a parent applies to all children.
  • Exclusive (OR) children: one active; parallel (AND) children: all active, run in numbered order.
  • Execution order matters when parallel states share data; give each variable one writer.
  • A history junction resumes the last active child instead of the default one.
  • Use subcharts and atomic subcharts to keep big charts readable and reusable.

Next: Temporal logic, MATLAB in charts and truth tables