| Stateflow® | ![]() |
| On this page… |
|---|
You can attach actions to states and transitions through the syntax of their labels. States specify actions through five action types: entry, during, exit, bind, and on event_name. Transitions specify actions through four action types: event trigger, condition, condition action, and transition action.
States can have different action types, which include entry, during, exit, bind, and, on event_name actions. The actions for states are assigned to an action type using label notation with this general format:
name/ entry:entry actions during:during actions exit:exit actions bind:data_name, event_name on event_name:on event_name actions
Different state action types appear in this diagram.

After you enter the name in the state label, enter a carriage return and specify the actions for the state.
Note The order you use to enter action types in the label does not matter. |
This table summarizes the different state action types and what they do.
| 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 |
| on event_name | none | Executes when the state is active and it receives a broadcast of event_name |
| on after(n, event_name) | none | Executes when the state is active and after it receives n broadcasts of event_name |
| on before(n, event_name) | none | Executes when the state is active and before it receives n broadcasts of event_name |
| on at(n, event_name) | none | Executes when the state is active and it receives exactly n broadcasts of event_name |
| on every(n, event_name) | none | Executes when the state is active and upon receipt of every n broadcasts of event_name |
For a full description of entry, exit, during, bind, and on event_name actions, see the topics that follow. For more information about the after, before, at, and every temporal logic operators, see Using Temporal Logic in State Actions and Transitions.
Note In the preceding table, the temporal logic operators use the syntax of event-based temporal logic. For absolute-time temporal logic, the operators use a different syntax. For details, see Operators for Absolute-Time Temporal Logic. |
Entry actions are preceded by the prefix entry or en for short, followed by a required colon (:), followed by one or more actions. Separate multiple actions with a carriage return, semicolon (;), or a comma (,). If you enter the name and slash followed directly by actions, the actions are interpreted as entry action(s). This shorthand is useful if you are specifying entry actions only.
Entry actions are executed for a state when the state is entered (becomes active). In the preceding example in State Action Types, the entry action id = x+y is executed when the state A is entered by the default transition.
For a detailed description of the semantics of entering a state, see Entering a State and State Execution Example.
Exit actions are preceded by the prefix exit or ex for short, followed by a required colon (:), followed by one or more actions. Separate multiple actions with a carriage return, semicolon (;), or a comma (,).
Exit actions for a state are executed when the state is active and a transition out of the state is taken.
For a detailed description of the semantics of exiting a state, see Exiting an Active State and State Execution Example.
During actions are preceded by the prefix during or du for short, followed by a required colon (:), followed by one or more actions. Separate multiple actions with a carriage return, semicolon (;), or a comma (,).
During actions are executed for a state when it is active and an event occurs and no valid transition to another state is available.
For a detailed description of the semantics of executing an active state, see Executing an Active State and State Execution Example.
Bind actions are preceded by the prefix bind, followed by a required colon (:), followed by one or more events or data. Separate multiple data/events with a carriage return, semicolon (;), or a comma (,).
Bind actions bind the specified data and events to a state. Data bound to a state can be changed by the actions of that state or its children. Other states and their children are free to read the bound data, but they cannot change it. Events bound to a state can be broadcast only by that state or its children. Other states and their children are free to listen for the bound event, but they cannot send it.
Bind actions are applicable to a Stateflow chart whether the binding state is active or not. In the preceding example in State Action Types, the bind action bind: id, time_out for state A binds the data id and the event time_out to state A. This forbids any other state (or its children) in the Stateflow chart from changing id or broadcasting event time_out.
If another state includes actions that change data or send events that are bound to another state, a parsing error results. The following example demonstrates a few of these error conditions:

Binding a function-call event to a state also binds the function-call subsystem that it calls. In this case, the function-call subsystem is enabled when the binding state is entered and disabled when the binding state is exited. For a detailed description of this feature, see Using Bind Actions to Control Function-Call Subsystems.
On event_name actions are preceded by the prefix on, followed by a unique event, event_name, followed by one or more actions. Separate multiple actions with a carriage return, semicolon (;), or a comma (,). You can specify actions for more than one event by adding additional on event_name lines for different events. If you want different events to trigger different actions, enter multiple on event_name action statements in the state's label, each specifying the action for a particular event or set of events, for example:
on ev1: action1(); on ev2: action2();
On event_name actions execute when the state is active and the event event_name is received by the state. This action coincides with execution of during actions for the state.
For a detailed description of the semantics of executing an active state, see Executing an Active State.
In State Action Types, you see how you can attach actions to the label for a state. You can also attach actions to a transition through its label. Transitions can have different action types, which include event triggers, conditions, condition actions, and transition actions. The actions for transitions are assigned to an action type using label notation with the following general format:
event_trigger[condition]{condition_action}/transition_action
The following example shows examples of transition action types:

In transition label syntax, event triggers appear first as the name of an event. They have no distinguishing special character to separate them from other actions in a transition label. In the example in Transition Action Types, both transitions from state A have event triggers. The transition from state A to state B has the event trigger event2 and the transition from state A to state C has the event trigger event1.
Event triggers specify an event that causes the transition to be taken, provided the condition, if specified, is true. Specifying an event is optional. The absence of an event indicates that the transition is taken upon the occurrence of any event. Multiple events are specified using the OR logical operator (|).
In transition label syntax, conditions are Boolean expressions enclosed in square brackets ([]). In the example in Transition Action Types, the transition from state A to state C has the condition temp > 50.
A condition is a Boolean expression to specify that a transition occurs given that the specified expression is true. Follow these guidelines for defining and using conditions:
The condition expression must be a Boolean expression that evaluates to true (1) or false (0).
The condition expression can consist of any of the following:
Boolean operators that make comparisons between data and numeric values
A function that returns a Boolean value
An in(state_name) condition that evaluates to true when the state specified as the argument is active (see Checking State Activity)
Temporal logic conditions (see Using Temporal Logic in State Actions and Transitions)
The condition expression can call a graphical function, truth table function, or Embedded MATLAB function that returns a numeric value.
For example, [test_function(x, y) < 0] is a valid condition expression.
The condition expression should not call a function that causes the Stateflow chart to change state or modify any variables.
Boolean expressions can be grouped using & for expressions with AND relationships and | for expressions with OR relationships.
Assignment statements are not valid condition expressions.
Unary increment and decrement actions are not valid condition expressions.
In transition label syntax, condition actions follow the transition condition and are enclosed in curly braces ({}). In the example in Transition Action Types, the transition from state A to state C has the condition action func1(), a function call.
Condition actions are executed as soon as the condition is evaluated as true, but before the transition destination has been determined to be valid. If no condition is specified, an implied condition evaluates to true and the condition action is executed.
Note A condition is checked only if the event trigger (if any) is active. |
In transition label syntax, transition actions are preceded with a forward slash (/). In the example in Transition Action Types, the transition from state A to state B has the transition action data1 = 5.
Transition actions are executed when the transition is actually taken. They are executed after the transition destination has been determined to be valid, and the condition, if specified, is true. If the transition consists of multiple segments, the transition action is executed only when the entire transition path to the final destination is determined to be valid.
In State Action Types and Transition Action Types, you are introduced to the notation and meaning of the Stateflow action language types. In this topic, you see how Stateflow action language types interact when you execute this chart.

If the Stateflow chart wakes up, these steps occur:
If state A is active and the Stateflow chart receives the event event2, these steps occur:
If state A is active and the Stateflow chart receives the event event1, these steps occur:
The condition temp > 50 evaluates.
| If the condition is... | Then the remaining steps... | And the transition... |
|---|---|---|
| True (temp > 50) | Execute | Occurs |
| False (temp <= 50) | Do not execute | Does not occur |
The condition action call to the function func1() executes.
State C becomes active.
![]() | Using Actions in Stateflow Charts | Using Operations in Actions | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |