Main Content

Transition Between Exclusive States

Label Format for a State-to-State Transition

The following example shows the general label format for a transition entering a state.

Stateflow chart with states called S1 and S2.

A chart executes this transition as follows:

  1. When an event occurs, state S1 checks for an outgoing transition with a matching event specified.

  2. If a transition with a matching event is found, the condition for that transition ([condition]) is evaluated.

  3. If the condition is true, condition_action is executed.

  4. If there is a valid transition to the destination state, the transition is taken.

  5. State S1 is exited.

  6. The transition_action is executed when the transition is taken.

  7. State S2 is entered.

Transition from State to State with Events

The following example shows the behavior of a simple transition focusing on the implications of whether states are active or inactive.

Stateflow chart with states called On and Off.

Process a First Event

Initially, the chart is asleep. State On and state Off are OR states. State On is active. Event E_one occurs and awakens the chart, which processes the event from the root down through the hierarchy:

  1. The chart root checks to see if there is a valid transition as a result of E_one. A valid transition from state On to state Off is detected.

  2. State On exit actions (exitOn()) execute and complete.

  3. State On is marked inactive.

  4. The event E_one is broadcast as the transition action.

    This second event E_one is processed, but because neither state is active, it has no effect. If the second broadcast of E_one resulted in a valid transition, it would preempt the processing of the first broadcast of E_one. See Early Return Logic.

  5. State Off is marked active.

  6. State Off entry actions (entOff()) execute and complete.

  7. The chart goes back to sleep.

This sequence completes the execution of the Stateflow® chart associated with event E_one when state On is initially active.

Process a Second Event

Using the same example, what happens when the next event, E_one, occurs while state Off is active?

Stateflow chart with states called On and Off.

Initially, the chart is asleep. State Off is active. Event E_one occurs and awakens the chart, which processes the event from the root down through the hierarchy:

  1. The chart root checks to see if there is a valid transition as a result of E_one.

    A valid transition from state Off to state On is detected.

  2. State Off exit actions (exitOff()) execute and complete.

  3. State Off is marked inactive.

  4. State On is marked active.

  5. State On entry actions (entOn()) execute and complete.

  6. The chart goes back to sleep.

This sequence completes the execution of the Stateflow chart associated with the second event E_one when state Off is initially active.

Process a Third Event

Using the same example, what happens when a third event, E_two, occurs?

Stateflow chart with states called On and Off.

Notice that the event E_two is not used explicitly in this example. However, its occurrence (or the occurrence of any event) does result in behavior. Initially, the chart is asleep and state On is active.

  1. Event E_two occurs and awakens the chart.

    Event E_two is processed from the root of the chart down through the hierarchy of the chart.

  2. The chart root checks to see if there is a valid transition as a result of E_two. There is none.

  3. State On during actions (durOn()) execute and complete.

  4. The chart goes back to sleep.

This sequence completes the execution of the Stateflow chart associated with event E_two when state On is initially active.

Tip

Avoid using undirected local event broadcasts. Undirected local event broadcasts can cause unwanted recursive behavior in your chart. Instead, send local events by using directed broadcasts. For more information, see Broadcast Local Events to Synchronize Parallel States.

During simulation, Stateflow charts can detect undirected local event broadcasts. To control the level of diagnostic action, open the Configuration Parameters dialog box and, in the Diagnostics > Stateflow pane, set the Undirected event broadcasts parameter to none, warning, or error. The default setting is warning. For more information, see Undirected event broadcasts (Simulink).

Transition from a Substate to a Substate with Events

This example shows the behavior of a transition from an OR substate to an OR substate.

Stateflow chart with top-level states called A and B. State A has substates A1 and A2. State B has substates B1 and B2.

Initially, the chart is asleep. State A.A1 is active. Condition C_one is true. Event E_one occurs and awakens the chart, which processes the event from the root down through the hierarchy:

  1. The chart root checks to see if there is a valid transition as a result of E_one. There is a valid transition from state A.A1 to state B.B1. (Condition C_one is true.)

  2. State A during actions (durA()) execute and complete.

  3. State A.A1 exit actions (exitA1()) execute and complete.

  4. State A.A1 is marked inactive.

  5. State A exit actions (exitA()) execute and complete.

  6. State A is marked inactive.

  7. The transition action, A, is executed and completed.

  8. State B is marked active.

  9. State B entry actions (entB()) execute and complete.

  10. State B.B1 is marked active.

  11. State B.B1 entry actions (entB1()) execute and complete.

  12. The chart goes back to sleep.

This sequence completes the execution of this Stateflow chart associated with event E_one.