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 Action | Abbreviation | Description |
---|---|---|
entry | en | Executes when the state becomes active. |
exit | ex | Executes when the state is active and a transition out of the state occurs. |
during | du | Executes when the state is active and a specific event occurs. |
bind | none | Binds an event or data object so that only that state and its children can broadcast the event or change the data value. |
| none | Executes when the state is active and it receives a broadcast of
event_name . |
| none | Executes when a message message_name is
available. |
| none | Executes when:
For more information, see |
| none | Executes when:
For more information, see |
| none | Executes when:
For more information, see |
| none | Executes:
For more information, see |
entry
ActionsEntry 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
ActionsExit 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
ActionsDuring 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
ActionsYou 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 Action | Reason for Parse Error |
---|---|
bind: id in state B | Only one state can change the data id , which is bound to
state A |
entry: time_out in state C | Only 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
ActionsOn 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();
ev1
and ev2
occur at the same time, then
action1()
executes first and action2()
executes
second. See Execution of a Stateflow Chart.