| Contents | Index |
Temporal logic controls execution of a Stateflow chart in terms of time. In state actions and transitions, you can use two types of temporal logic: event-based and absolute-time. Event-based temporal logic keeps track of recurring events, and absolute-time temporal logic defines time periods based on the simulation time of your chart. To operate on these recurring events or simulation time, you use built-in functions called temporal logic operators.
These rules apply to the use of temporal logic operators:
You can use any explicit or implicit event as a base event for a temporal operator. A base event is a recurring event on which a temporal operator operates.
For a chart with no input events, you can use the tick or wakeup event to denote the implicit event of a chart waking up.
Temporal logic operators can appear only in:
State actions
Transitions that originate from states
Transition segments that originate from junctions when the full transition path connects two states
Note This restriction means that you cannot use temporal logic operators in default transitions or flow graph transitions. |
Every temporal logic operator has an associated state: the state in which the action appears or from which the transition originates.
You must use event notation (see Notations for Event-Based Temporal Logic) to express event-based temporal logic in state actions.
For event-based temporal logic, use the operators as described below.
These examples illustrate usage of event-based temporal logic in state actions and transitions.
| Operator | Usage | Example | Description |
|---|---|---|---|
after | State action (on after) | on after(5, CLK): status('on'); | A status message appears during each CLK cycle, starting 5 clock cycles after activation of the state. |
after | Transition | ROTATE[after(10, CLK)] | A transition out of the associated state occurs only on broadcast of a ROTATE event, but no sooner than 10 CLK cycles after activation of the state. |
before | State action (on before) | on before(MAX, CLK): temp++; | The temp variable increments once per CLK cycle until the state reaches the MAX limit. |
before | Transition | ROTATE[before(10, CLK)] | A transition out of the associated state occurs only on broadcast of a ROTATE event, but no later than 10 CLK cycles after activation of the state. |
at | State action (on at) | on at(10, CLK): status('on'); | A status message appears at exactly 10 CLK cycles after activation of the state. |
at | Transition | ROTATE[at(10, CLK)] | A transition out of the associated state occurs only on broadcast of a ROTATE event, at exactly 10 CLK cycles after activation of the state. |
every | State action (on every) | on every(5, CLK): status('on'); | A status message appears every 5 CLK cycles after activation of the state. |
temporalCount | State action (during) | du: y = mm[temporalCount(tick)]; | This action counts and returns the integer number of ticks that have elapsed since activation of the state. Then, the action assigns to the variable y the value of the mm array whose index is the value that the temporalCount operator returns. |
You can use one of two notations to express event-based temporal logic.
Use event notation to define a state action or a transition condition that depends only on a base event.
Event notation follows this syntax:
tlo(n, E)[C]
where
tlo is a Boolean temporal logic operator (after, before, at, or every)
n is the occurrence count of the operator
E is the base event of the operator
C is an optional condition expression
Use conditional notation to define a transition condition that depends on base and nonbase events.
Conditional notation follows this syntax:
E1[tlo(n, E2) && C]
where
E1 is any nonbase event
tlo is a Boolean temporal logic operator (after, before, at, or every)
n is the occurrence count of the operator
E2 is the base event of the operator
C is an optional condition expression
| Notation | Usage | Example | Description |
|---|---|---|---|
Event | State action (on after) | on after(5, CLK): temp = WARM; | The temp variable becomes WARM 5 CLK cycles after activation of the state. |
Event | Transition | after(10, CLK)[temp == COLD] | A transition out of the associated state occurs if the temp variable is COLD, but no sooner than 10 CLK cycles after activation of the state. |
Conditional | Transition | ON[after(5, CLK) && temp == COLD] | A transition out of the associated state occurs only on broadcast of an ON event, but no sooner than 5 CLK cycles after activation of the state and only if the temp variable is COLD. |
Note You must use event notation in state actions, because the syntax of state actions does not support the use of conditional notation. |
For absolute-time temporal logic, use the operators as described below.
Use the keyword sec to define simulation time that has elapsed since activation of a state. This keyword is valid only in state actions and in transitions that originate from states.
The following continuous-time chart defines two absolute time delays in transitions. (See Modeling Continuous-Time Systems in Stateflow Charts for information about modeling continuous-time systems.)

Chart execution occurs as follows:
When the chart awakens, the state Input activates first.
After 5.33 seconds of simulation time pass, the transition from Input to Output occurs.
The state Input deactivates, and the state Output activates.
After another 10.5 seconds of simulation time pass, the transition from Output to Input occurs.
The state Output deactivates, and the state Input activates.
Steps 2 through 5 repeat, until the simulation ends.
In the following model, the Step block provides a unit step input to the chart:

The chart determines when the input u equals 1.

| If the input equals 1... | A transition occurs from... |
|---|---|
| Before t = 2 | Start to Fast |
| Between t = 2 and t = 5 | Start to Good |
| After t = 5 | Start to Slow |
Use absolute-time temporal logic instead of the implicit tick event for these reasons:
Delay expressions that use absolute-time temporal logic are independent of the sample time of the model. However, the tick event is dependent on sample time.
Absolute-time temporal logic works in charts that have function-call input events. However, the tick event does not work in charts with function-call inputs.
These examples illustrate usage of absolute-time temporal logic in state actions and transitions.
| Operator | Usage | Example | Description |
|---|---|---|---|
after | State action (on after) | on after(12.3, sec): temp = LOW; | The temp variable becomes LOW after 12.3 seconds of simulation time have passed, since activation of the state. |
after | Transition | after(12.34, sec) | A transition out of the associated state occurs after 12.34 seconds of simulation time have passed, since activation of the state. |
before | Transition | [temp > 75 && before(12.34, sec)] | A transition out of the associated state occurs if the variable temp exceeds 75 and fewer than 12.34 seconds have elapsed since activation of the state. |
temporalCount | State action (exit) | ex: y = temporalCount(sec); | This action counts and returns the number of seconds of simulation time that pass between activation and deactivation of the state. |
The sf_boiler model shows the use of absolute-time temporal logic to implement a bang-bang controller. To run the model:
Click sf_boiler or type sf_boiler at the MATLAB command prompt.
Start simulation of the model.
You can use absolute-time temporal logic in a chart that resides in a conditionally executed subsystem. (See Creating Conditional Subsystems in the Simulink documentation for details.) When the subsystem is disabled, the chart becomes inactive and the temporal logic operator pauses while the chart is asleep. The operator does not continue to count simulation time until the subsystem is reenabled and the chart is awake.
Suppose that your model has an enabled subsystem that contains a chart with the after operator. In the subsystem, the States when enabling parameter is set to held.
| Model with Enabled Subsystem | Chart in Enabled Subsystem |
|---|---|
|
|
The Signal Builder block provides the following input signal to the subsystem.

The total time elapsed in an enabled state (both A and B) is as follows.

When the input signal enables the subsystem at time t = 0, the state A becomes active, or enabled. While the state is active, the time elapsed increases. However, when the subsystem is disabled at t = 2, the chart goes to sleep and state A becomes inactive.
For 2 < t < 6, the time elapsed in an enabled state stays frozen at 2 seconds because neither state is active. When the chart wakes up at t = 6, state A becomes active again and time elapsed starts to increase. The transition from state A to state B depends on the time elapsed while state A is enabled, not on the simulation time. Therefore, state A stays active until t = 9, so that the time elapsed in that state totals 5 seconds.
When the transition from A to B occurs at t = 9, the output value y changes from 0 to 1.

This model behavior applies only to subsystems where you set the Enable block parameter States when enabling to held. If you set the parameter to reset, the Stateflow chart reinitializes completely when the subsystem is reenabled. In other words, default transitions execute and any temporal logic counters reset to 0.
If a Stateflow chart has a discrete sample time, any action in the chart occurs at integer multiples of this sample time.
Suppose you have a chart with a discrete sample time of 0.1 seconds:

State A becomes active at time t = 0, and the transition to state B occurs at t = 2.2 seconds. This behavior applies because the Simulink solver does not wake the chart at exactly t = 2.15 seconds. Instead, the solver wakes the chart at integer multiples of 0.1 seconds, such as t = 2.1 and 2.2 seconds.
If you use the at operator with absolute-time temporal logic, an error message appears when you try to simulate your model. Use the after operator instead.
Suppose that you want to define a time delay using the transition at(5.33, sec).

Change the transition to after(5.33, sec), as shown below.

If you use the every operator with absolute-time temporal logic, an error message appears when you try to simulate your model. Use an outer self-loop transition with the after operator instead.
Suppose that you want to print a status message for an active state every 2.5 seconds during chart execution, as shown in the state action of Check_status.

Replace the state action with an outer self-loop transition, as shown below.

You must also add a history junction in the state so that the chart remembers the state settings prior to each self-loop transition. (See Recording State Activity with History Junctions.)
![]() | Broadcasting Events to Synchronize States | Detecting Changes in Data Values | ![]() |

Learn how engineers use Stateflow to model state machines in their Simulink models.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |