Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 36 ยท Stateflow

Stateflow Temporal Logic, MATLAB Actions and Truth Tables

Use Stateflow temporal logic (after, before, at, every, temporalCount), MATLAB or C actions, state transition tables and truth tables to describe timing and decisions.

Many control rules are about time: "stay in Heating for at least 30 seconds", "blink every half second", "give up after five failed attempts". Stateflow has built-in temporal logic operators for that, so you do not need a counter variable for each rule. This lesson also covers the two table forms of a chart, the State Transition Table and the Truth Table, and shows how to choose between them. Stateflow is not available on this site, so Python stand-ins (labelled) mimic the rules.

Temporal logic operators

Temporal operators go in transition labels or action expressions and count either chart ticks (time steps in which the chart woke up), events, or seconds of simulation time.

Operator Meaning (n = a number)
after(n, E) True once the event E has occurred n times since the state became active (true afterwards too)
before(n, E) True while E has occurred fewer than n times since the state became active
at(n, E) True only at the n-th occurrence of E
every(n, E) True at every n-th occurrence of E (n, 2n, 3n...)
temporalCount(E) The number of occurrences of E since the state became active

E can be a named event, tick (each chart wake-up) or sec (absolute simulation time; also msec, usec). Examples:

TEXT
after(5, sec)              // after five seconds in this state
[after(3, tick) && ready]  // the third tick onward, and ready is true
every(10, tick)            // at tick 10, 20, 30... in this state

The counter restarts each time the state is entered, because the count is relative to when the state became active. This is exactly the right behavior for timeouts.

Notes:

  • Seconds require a chart with a discrete or inherited sample time so that the chart knows the step; Stateflow warns you when time-based operators have no sample time.
  • In generated code the operator needs a counter per state, so a timeout in seconds is converted to a whole number of steps of the base sample time. Pick timeouts that are a multiple of the step (see the second stand-in below).
  • Use after for timeouts, every for periodic tasks, at for a single action at a given count.
stateDiagram-v2
    [*] --> Red
    Red --> Green: after(30, sec)
    Green --> Yellow: after(25, sec)
    Yellow --> Red: after(5, sec)

A traffic light with three timed states. No counter variable is needed.

A stand-in for after and every

The stand-in implements the semantics with an explicit counter reset on state entry. It is not Stateflow, but the rule is the same.

Python Stand-in: after(n, tick) and every(n, tick)

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.

Using seconds: converting time to ticks

Because timeouts in seconds become a number of steps, you can check how a timeout behaves for a given step size. This stand-in follows the conversion idea (rounding to a whole number of steps).

Python Stand-in: how many steps is a timeout in seconds?

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.

Actions in MATLAB or C

The action language is chosen in chart properties. In a MATLAB chart:

  • Assign with =, index from 1, use ~ for not, ~= for not equal, &&/|| in conditions.
  • Call MATLAB functions and operators on matrices, use if, for, while blocks inside actions (indent as text).
  • Use send(E) to broadcast an event and in(StateName) to test whether a state is active (for example [in(Mode.Run)]).

In a C chart: ==, !=, !, zero-based indexing, and the same functions as C (abs, sqrt...). If you plan to generate readable C for a microcontroller, C action language often maps more directly; MATLAB charts also generate C, just through a translation.

You cannot freely mix the two inside one chart; choose once and keep to it. Use . for substates: Machine.Run.Fast.

State Transition Table

A State Transition Table is a chart type shown as a spreadsheet instead of a diagram. Each row is: From, To, Condition, Actions, Description. States have columns for entry, during and exit. A row with an empty From is a default transition, a From of a state means that state. The behavior is the same as a graphical chart, and you can switch to the graphical view to see the generated diagram (in current releases).

Prefer the table when:

  • There are many states with a regular pattern (menus, protocols), because rows are easy to review and diff.
  • You need to review logic with non-experts who prefer tables.
  • You want to avoid layout work.

Prefer a diagram for hierarchy and parallel decomposition, which tables express less naturally.

Truth Table

A Truth Table (as a block or a function inside a chart) is a pure decision function. You fill in conditions (rows), decisions (columns) and actions. Decisions are evaluated in order and the first that matches runs its actions. The special - entry means "don't care", and T/F mean true/false. Stateflow can check a truth table for completeness and overlap with Check for Undefined / Overlap, which is a strong reason to use it.

Example: a window-lift rule with conditions up_pressed, down_pressed, at_top:

Condition D1 D2 D3 D4
up_pressed T F F T
down_pressed F T F T
at_top F - - -
Action move up move down stop stop (conflict)

The stand-in below implements ordered decision evaluation and a brute-force completeness check, the same idea as Stateflow's table checker.

Python Stand-in: truth table with first-match decisions and a completeness check

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.

Run it and read the result: it reports the case where the up button is pressed while the window is already at the top (the table has no row for it), and nothing overlaps. In Stateflow the checker flags the same two kinds of problems before you simulate.

Choosing the form

Need Best form
Modes, sequences, hierarchy, parallel behavior Graphical chart
Many similar rows, easy review State Transition Table
Pure combinational decisions, completeness check Truth Table
One-off formula A MATLAB Function block or an expression

Common mistakes

  • Counting ticks when you mean seconds: if you change the step size, a tick count changes meaning. Use sec for physical durations.
  • Expecting after to count across state visits: it restarts on every entry.
  • Timeouts that are not multiples of the step. They round to whole steps.
  • Mixing = and == (in C action language) or ~= and != (in MATLAB).
  • A truth table that does not cover all combinations: always run the completeness check.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does after(5, sec) on a transition mean?

    The count starts when the state becomes active, so it measures time in this state; at(n) and every(n) are the other forms.
  2. 2. Which operator is true only at the n-th occurrence of an event?

    at(n, E) is true exactly at the n-th occurrence, while after(n, E) stays true from then on.
  3. 3. When is a Truth Table the better choice than a graphical chart?

    Truth tables are decision functions and Stateflow can check them for undefined and overlapping combinations.
  4. 4. What happens to the temporal count when a state is exited and entered again?

    Temporal logic counts relative to the last time the state became active.

Summary

  • Temporal operators (after, before, at, every, temporalCount) count ticks, events or seconds since the state became active.
  • Choose seconds for physical durations and keep timeouts a multiple of the sample time.
  • Pick MATLAB or C action language once per chart; use in(State) and send(E) in MATLAB charts.
  • State Transition Tables suit regular state logic; Truth Tables suit pure decisions and can be checked for gaps and overlaps.
  • The Python code above is a stand-in; use the real Stateflow editor for actual models.

Next: PID control design and tuning