主要内容

Create Transitions Between States

Transitions in Stateflow charts define how a system moves from one state to another in response to events, conditions, or time-based criteria.

A transition label in Stateflow® consists of four optional parts:

  • Event triggers specify when the chart evaluates the transition.

  • Condition guards determine whether the transition can occur.

  • Condition actions execute when the condition is true.

  • Transition actions execute when the chart takes the transition.

A Stateflow chart that shows each part of a transition label.

Transition Types

Transitions connect a source state to a destination state. When the chart takes a transition, the source state becomes inactive and the destination state becomes active.

Default Transitions

A default transition specifies the initial state that becomes active when its parent state activates. Default transitions have no source state, only a destination state.

Default transitions can only include condition actions and transition actions.

A default transition with a condition action and a transition action.

For example, this chart manages a temperature monitoring system with a state called MonitoringMode. When the system initializes, it starts a temperature counter. Next, the system turns off the heater and sends a temperature alert.

Self-Loop Transitions

A transition that begins and ends at the same state is called a self-loop transition. Use self-loop transitions to respond to or send events or update data without changing to a different state. For more information, see Self-Loop Transition.

A state that includes a self-loop transition.

In this example, the default transition sets count equal to 0, and then enters state A. After entering state A, count increases by 1. After count is greater than 3, the chart exists the state, sends the event, adds 1 to count and then re-enters state A.

Create Conditional Transitions

Condition guards control whether a transition is valid based on the evaluation of an expression. Place condition guards in square brackets.

Transition condition without event.

In this chart, the transition from state A to state B occurs when temp is greater than 90.

Execute Transitions Guarded by an Event

When an event occurs, the chart wakes up and evaluates the transition that is guarded by an event in this order:

  1. The chart evaluates the condition guard of each transition, in order.

  2. If the condition is true, the chart executes the actions of a transition.

  3. The source state becomes inactive and executes any exit actions.

  4. The destination state becomes active and executes any entry actions.

If multiple transitions from the same state are valid for a given event, the chart takes the first valid transition based on execution order. For more information, see Use Events to Execute Charts.

Transition condition with event.

In this chart, the transition from state A to state B only occurs once the TOO_HOT event occurs and temp is above 90.

Transition Actions

Transition actions execute when t, after the exit actions of the source state and before the entry actions of the destination state. Use a forward slash to specify transition actions.

A transition with a transition action.

In this chart, when the event TOO_HOT occurs, the chart exits state A and performs the exit action lastStateChangeTime = t;. Then, before entering state B, the chart executes the transition action and sets the value of status to "WARNING".

See Also

Topics