State Action Types

States can have different action types, including entry, during, exit, bind, and, on actions. You specify the actions for a state by using a state action label with this overall format:

name/
entry:entry actions
during:during actions
exit:exit actions
bind:data_name, event_name
on event_name:on event_name actions
on message_name:on message_name actions

Enter actions of different types on separate lines after the name of the state. You can enter these actions in any order. If you do not specify the action type explicitly for a statement, the chart treats that statement as a combined entry,during action.

For example, this chart contains various state action types.

This table summarizes the different state action types.

State ActionAbbreviationDescription
entryenExecutes when the state becomes active.
exitexExecutes when the state is active and a transition out of the state occurs.
duringduExecutes when the state is active and a specific event occurs.
bindnoneBinds an event or data object so that only that state and its children can broadcast the event or change the data value.

on event_name

noneExecutes when the state is active and it receives a broadcast of event_name.

on message_name

noneExecutes when a message message_name is available.

on after(n,event_name)

on after(n,sec)

on after(n,msec)

on after(n,usec)

none

Executes when:

  • the event event_name has occurred at least n times since the associated state became active.

  • at least n units of time have elapsed since the associated state became active.

For more information, see after.

on at(n,event_name)

on at(n,sec)

none

Executes when:

  • the event event_name has occurred exactly n times since the associated state became active.

  • exactly n seconds have elapsed since the associated state became active.

For more information, see at.

on before(n,event_name)

on before(n,sec)

on before(n,msec)

on before(n,usec)

none

Executes when:

  • the event event_name has occurred fewer than n times since the associated state became active.

  • fewer than n units of time have elapsed since the associated state became active.

For more information, see before.

on every(n,event_name)

on every(n,sec)

none

Executes:

  • every nth occurrence of the event event_name since the associated state became active.

  • every n seconds since the associated state became active.

For more information, see every.

entry Actions

Entry actions are executed when a state becomes active. Entry actions consist of the prefix entry (or the abbreviation en) followed by a colon (:) and one or more actions. To separate multiple entry actions, use semicolons or commas. You can also enter the actions on separate lines.

In the preceding example, the entry action id = x+y executes when the chart takes the default transition and state A becomes active. See Enter a Chart or State.

exit Actions

Exit actions are executed when a state is active and a transition out of the state occurs. Exit actions consist of the prefix exit (or the abbreviation ex) followed by a colon (:) and one or more actions. To separate multiple exit actions, use semicolons or commas. You can also enter the actions on separate lines.

In the preceding example, the exit action time_out executes when the chart takes one of the transitions from state A to state B or C. See Exit a State.

during Actions

During actions are executed when a state is active, an event occurs, and no valid transition to another state or the current state is available. During actions consist of the prefix during (or the abbreviation du) followed by a colon (:) and one or more actions. To separate multiple during actions, use semicolons or commas. You can also enter the actions on separate lines.

In the preceding example, the during action switch_on() executes whenever the state C is active because there are no valid transitions to another state. See Execution of a Stateflow Chart.

bind Actions

You can bind the data and events to a state by using a bind action. A bind action consists of the prefix bind followed by a colon (:) and one or more events or data. To separate multiple events and data, use semicolons or commas. You can also enter the events and data on separate lines.

Only a state and its children can change data or broadcast events bound to that state. Other states can read the bound data or listen for the bound event, but they cannot change the bound data or send the bound events.

Bind actions apply to a chart whether the binding state is active or not. In the preceding example, the bind action bind: id, time_out for state A binds the data id and the event time_out to state A. This binding prevents any other state (or its children) in the chart from changing id or broadcasting event time_out.

If another state includes actions that change data or broadcast events that bind to another state, a parsing error occurs. This chart contains two state actions that produce parsing errors.

State ActionReason for Parse Error
bind: id in state BOnly one state can change the data id, which is bound to state A
entry: time_out in state COnly one state can broadcast the event time_out, which is bound to state A

Binding a function-call event to a state also binds the function-call subsystem that it calls. The function-call subsystem is enabled when the binding state is entered and disabled when the binding state is exited. For more information about this behavior, see Control Function-Call Subsystems by Using bind Actions.

Bind actions are supported only in Stateflow® charts in Simulink® models.

on Actions

On actions are executed when the state is active and it receives an event or message. On actions consist of the prefix on followed by a unique event event_name or message message_name, a colon (:), and one or more actions. To separate multiple on actions, use semicolons or commas. You can also enter the actions on separate lines.

You can specify actions for more than one event or message. For example, if you want different events to trigger different actions, enter multiple on action statements in the state action label:

on ev1: action1();
on ev2: action2();
If multiple events occur at the same time, the corresponding on actions execute in the order that they appear in the state action label. For instance, in the previous example, if events ev1 and ev2 occur at the same time, then action1() executes first and action2() executes second. See Execution of a Stateflow Chart.

Related Topics