Stateflow | ![]() ![]() |
Defining State Actions
Stateflow allows you to specify actions that occur when a state machine enters a state, exits a state, and while a state is active.
Defining Entry Actions
An entry action is an action executed by a state machine when it enters a particular state as the result of taking a transition to that state. To specify the entry action to be taken for a given state, add an entry block to the state's label. An entry
block begins on a new line and consists of the entry action keyword, entry
or en
, followed by a colon, followed by one or more action statements on one or more lines. You must separate statements on the same line by a comma or semicolon. See Action Language for information on writing action statements.
Note You can also begin a state's entry action on the same line as the state's name. In this case, begin the entry action with a forward slash (/) instead of the entry keyword. |
Defining Exit Actions
An exit action is an action executed by a state machine when it exits a state as the result of taking a transition away from the state or the occurrence of an event (see Defining On-Event Actions below). To specify an exit action for a state, add an exit block to the state's label. The format of an exit block is the same as that of an entry block except that the exit block begins with the keyword exit
or ex
.
Defining During Actions
A during action is an action that a state machine executes while a state is active, that is, after the state machine has entered the state and while there is no valid transition away from the state. To specify a during action, add a during block to the state's label. A during block has the same format as an entry block except that it begins with the keyword during
or dur
.
Defining On-Event Actions
An on-event action is an action that a state machine takes when a state is active and one or more events of a specific type occur. (See Defining Events for information on defining and using events to drive a state machine.) To specify an event handler for a state, add an on-event block to the state. An on-event block has the same format as an entry action block except that it begins with the keyword, on
, followed by the name of the event, followed by a colon, for example
on ev1: exit();
A state machine can respond to multiple events, with either the same or different actions, when a state is active. If you want more than one type of event to trigger the same action, specify the keyword as on events
, where events
is a comma-separated list of the events that trigger the actions, for example,
on ev1, ev2: exit();
If you want different events to trigger different actions, enter multiple event blocks 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 ev3, ev4: exit();
Note Use a during block to specify actions that you want a state machine to take in response to any visible event that occurs while the machine is in a particular state (see Defining During Actions). |
![]() | Naming States | Outputting State Activity to Simulink | ![]() |