| Contents | Index |
| On this page… |
|---|
Build It Yourself or Use the Supplied Model Design Considerations for Defining State Actions and Variables |
To implement the state actions yourself, work through the exercises in this section. Otherwise, open the supplied model to see how the actions should appear in the states. Enter this command at the MATLAB prompt:
run(docpath(fullfile(docroot,'toolbox','stateflow','gs','examples','Stage3Actions.mdl')))
The following sections describe the decisions you make for defining state actions and variables.
At this stage of the workflow for developing Stateflow charts, you must determine if any of your states require local or persistent variables. If so, you define these data elements using the Stateflow Editor or the Model Explorer, as described in Adding Data in the Stateflow User's Guide.
The states in the Air Controller chart do not require local or persistent data, only the input and output data that you defined in Defining the Inputs and Outputs.
During simulation of a Stateflow chart, states can perform actions while they are active. Often, actions are used to manipulate data, using a variety of constructs such as binary, bitwise, unary, assignment, and type cast operators (see Using Actions in Stateflow Charts in the Stateflow User's Guide).
When building the Air Controller chart, you need to determine whether any states should perform actions. Some charts do not use state actions at all, but instead perform actions only during the transitions from state to state. Other charts require both types of state actions.
For the Air Controller chart, think about whether data values need to be initialized or modified during any of its modes of operation. Recall that the chart receives the air temperature of the plant as the input temp from the Physical Plant subsystem. The chart then uses this value to activate fans if necessary to cool the air. Based on how many fans are running, the chart sets a value that indicates speed of airflow, which it sends at each time step to the Simulink subsystem as the output airflow. The Air Controller does not modify the value of temp, but does need to update the value of airflow.
The next consideration is when to update, and for that matter, initialize the value of airflow. If the when translates to a mode of operation, the action should likely be performed by the state that represents that mode of operation. Here is the analysis for the Air Controller chart:
| Action | When | How |
|---|---|---|
| Initialize airflow to 0. | Before simulation | Set an initial value when you first define airflow (as you did in Defining the Inputs and Outputs). |
| Set airflow to 0. | Whenever power is off | Add an action in the state PowerOff. |
| Update airflow to 0, 1, or 2, based on how many fans are running. | Whenever power is on | Add an action in the state SpeedValue, which becomes active concurrently with FAN1 and FAN2 when the state PowerOn is active. |
States perform actions at different phases of their execution cycle from the time they become active to the time they become inactive. Three basic state actions are:
| Type of Action | When Executed | How Often Executed While State Is Active |
|---|---|---|
| Entry | When the state is entered (becomes active) | Once |
| During | While the state is active and no valid transition to another state is available | At every time step |
| Exit | Before a transition is taken to another state | Once |
For example, you can use entry actions to initialize data, during actions to update data, and exit actions to configure data for the next transition. (There are other types of state actions, but they involve concepts that go beyond the scope of this guide. For more information, see Using Actions in Stateflow Charts in the Stateflow User's Guide.)
Based on the requirements in Deciding Whether to Use State Actions, you will write the following state actions for the Air Controller chart:
Entry action in state PowerOff to set airflow to 0
During action in state SpeedValue to calculate the value of airflow at every time step
The syntax for entry actions is
entry:one or more actions; en:one or more actions;
To write the entry action for PowerOff, follow these steps:
Open the model Stage2States — either the one you created in the previous exercises or the supplied model for stage 2.
To open the supplied model, enter the following command at the MATLAB prompt:
run(docpath(fullfile(docroot,'toolbox','stateflow','gs','examples','Stage2States.mdl')))
Save the model as Stage3Actions in your local work folder.
In Stage3Actions, double-click the Air Controller block to open the Stateflow chart.
Click inside the PowerOff state after the last letter of its name label to get a blinking text cursor.
Press the Enter key and type
entry: airflow = 0;
Your chart should look like this:

Save Stage3Actions, but leave the chart open for the next exercise.
The syntax for during actions is
during:one or more actions; du:one or more actions;
The during action for SpeedValue uses a Boolean expression to determine whether zero, one, or two fans are running at each time step.
To write the during action for SpeedValue, follow these steps:
Click inside the SpeedValue state after the last letter of its name label to get a blinking text cursor.
Press the Enter key and type
during: airflow = in(FAN1.On) + in(FAN2.On);
Your chart should look like this:

The Boolean expression in(FAN1.On) is true — and its value equals 1 — if the On state of FAN1 is active. If FAN1 is not on — that is, its Off state is active or power is off — then in(FAN1.On) equals 0. Similarly, the value of in(FAN2.On) represents whether FAN2 is on or off. Therefore, the sum of these Boolean expressions indicates whether 0, 1, or 2 fans are operating during each time step.
Save the model Stage3Actions.
Where to go next. Now you are ready to specify conditions for when state-to-state transitions occur. See Adding the Transitions.
![]() | Defining State Actions and Variables | Defining Transitions Between States | ![]() |

Learn how engineers use Stateflow to model state machines in their Simulink models.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |