| Stateflow User's Guide | ![]() |
State Labels
The label for a state appears on the top left corner of the state rectangle with the following general format:
name/entry:entry actionsduring:during actionsexit:exit actionsbind:events,dataonevent_name:on event_name actions
The following example demonstrates the components of a state label.
Each of the above actions is described in the subtopics that follow. For more information on state actions, see the following topics:
entry, during, exit, and on event_name actions are taken.
State Name
A state label starts with the name of the state followed by an optional / character. In the preceding example, the state names are On and Off. Valid state names consist of alphanumeric characters and can include the underscore (_) character, for example, Transmission or Green_on.
The use of hierarchy provides some flexibility in the naming of states. The name that you enter as part of the label must be unique when preceded by the hierarchy of its ancestor states. The name stored in the data dictionary is the text you enter as the label on the state, preceded by the hierarchy of its parent states separated by periods. Each state can have the same name appear in the label of the state, as long as their full names within the data dictionary are unique. Otherwise, the parser indicates an error.
The following example shows how hierarchy supports unique naming of states.
Each of these states has a unique name because of its location in the hierarchy of the Stateflow diagram. Although the name portion of the label on these states is not unique, when the hierarchy is prefixed to the name in the data dictionary, the result is unique. The full names for these states as seen in the data dictionary are as follows:
State Actions
After the name, you enter optional action statements for the state with a keyword label that identifies the type of action. You can specify none, some, or all of them. The colon after each keyword is required. The slash following the state name is optional as long as it is followed by a carriage return.
For each type of action, you can enter more than one action by separating each action 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 enter the name and slash followed directly by actions, the actions are interpreted as entry action(s). This shorthand is useful if you are only specifying entry actions.
Entry Action. Preceded by the prefix entry or en for short. In the preceding example, state On has entry action on_count=0. This means that the value of on_count is reset to 0 whenever state On becomes active (entered).
During Action. Preceded by the prefix during or du for short. In the preceding label example, state On has two during actions, light_on() and on_count++. These actions are executed whenever state On is already active and any event occurs.
Exit Action. Preceded by the prefix exit or ex for short. In the preceding label example, state Off has the exit action light_off(). If the state Off is active, but becomes inactive (exited), this action is executed.
On Event_Name Action. Preceded by the prefix on event_name, where event_name is a unique event. In the preceding label example, state On has an on power_outage action. If state On is active and the event power_outage occurs, the action handle_outage() is executed.
Bind Action. Preceded by the prefix bind. In the preceding label example, the data on_count is bound to the state On. This means that only the state On or a child of On can change the value of on_count. Other states, such as the state Off, can use on_count in its actions, but it cannot change its value in doing so.
| State Decomposition | Transitions | ![]() |
© 1994-2005 The MathWorks, Inc.