Main Content

Exit a State

When there is a valid transition out of a state, that state is marked for exit. A state is marked for exit in one of these ways:

  • The outgoing transition originates at the state boundary.

  • The outgoing transition crosses the state boundary.

  • The destination state is a parallel state child of an activated state.

Workflow for Exiting a State

This flow chart shows the progression of events in Stateflow® for exiting a state. In this flow chart, the current state refers to the state in which a decision or a process is taking place.

Flow chart that shows the steps for exiting a state.

Exit Actions

Exit actions for a state execute when the state is active and a valid transition from the state exists. A state performs its exit actions before becoming inactive.

Exit actions are preceded by the prefix exit or ex, followed by a required colon (:), and then followed by one or more actions. Separate multiple actions with a carriage return, semicolon (;), or a comma (,).

Note

If the current state is not the source of the transition path, Stateflow exits the current state and completes its exit actions. The parent then becomes the current state. This cycle is repeated until the current state is the source of the transition.

Exit a State Example

In this example, the Stateflow chart is initialized and the entry actions are performed for StateA and StateA1. For this chart, the during actions for this chart have occurred twice. A new time step occurs, and then the chart wakes up.

By following the Workflow for Stateflow Chart Execution and the Workflow for Evaluating Transitions, StateB has been marked for entry. StateA is the source of the transition. At this time step x = 5, y = 2, and z = 0.

Stateflow chart with top-level states called StateA and StateB. StateA has substates StateA1 and StateA2. StateB has substates StateB1 and StateB2.

By following the flow chart for state exit actions until the chart goes to sleep, the execution steps for this chart are in this order:

  1. StateA is not a superstate of StateB.

  2. Perform the exit actions of StateA and mark StateA as inactive. Now z = 1.

  3. StateA does not have a parent state.

  4. Go to Entry Actions.

These steps complete the exit workflow for StateA. However, the chart is not yet asleep.

Perform the Workflow for Entering a Chart or State for StateB to complete the time step.

Exit a State by Using Supertransitions

A supertransition is a transition between different levels in a chart. A supertransition can be between a state in a top-level chart and a state in one of its substates, or between states residing in different substates. You can create supertransitions that span any number of levels in your chart.

When a state is exited through a supertransition, after the exit actions for the source of the transition are executed, its superstates are marked inactive and exit actions of the superstates are executed. In this example, StateA2 is marked for exit and StateB1 is marked for entry. At this point, x = 5, y = 5, and z = 0.

Stateflow chart with top-level states called StateA and StateB. StateA has substates StateA1 and StateA2. StateB has substates StateB1 and StateB2.

By following the Workflow for Entering a Chart or State until the chart goes to sleep, the execution steps for exiting the state StateA2 are in this order:

  1. StateA2 is not a superstate of the destination state (StateB1).

  2. Perform the exit actions for StateA2 and mark StateA2 as inactive.

  3. StateA2 does have a parent state, StateA.

  4. StateA is not a superstate of the destination state (StateB1).

  5. Perform the exit actions for StateA, and mark StateA as inactive.

  6. StateA does not have a parent state.

These actions complete the exit workflow for StateA2 and StateA. However, the chart is not yet asleep.

Perform the Workflow for Entering a Chart or State for StateB and StateB1 to complete the time step.

Related Topics