| Contents | Index |
| On this page… |
|---|
Build It Yourself or Use the Supplied Model Design Considerations for Defining the Interface Adding a Stateflow Block to a Simulink Model |
To implement the interface yourself, work through the exercises in this section. Otherwise, open the supplied model by entering this command at the MATLAB prompt:
run(docpath(fullfile(docroot,'toolbox','stateflow','gs','examples','Stage1Interface.mdl')))
The following sections describe the rationale for the input and output of the Stateflow chart.
Type of Input. Temperature of the physical plant
Rationale. The purpose of the chart is to control the air temperature in a physical plant. The goal is to maintain an ideal temperature of 120 degrees by activating one or two cooling fans if necessary. The chart must check the plant temperature over time to determine the amount of cooling required.
Properties of Input. The properties of the temperature input are as follows:
| Property | Value |
|---|---|
| Name | temp |
| Scope | Input |
| Size | Inherit from Simulink input signal for compatibility |
| Data type | Inherit from Simulink input signal for compatibility |
| Port | 1 |
| Watch in debugger | Enable |
Type of Output. Speed of airflow, based on how many fans are operating
Rationale. When the Simulink subsystem determines the temperature of the physical plant over time, it needs to account for the speed of the airflow. Airflow speed is directly related to the amount of cooling activity generated by the fans. As more fans are activated, cooling activity increases and air flows faster. To convey this information, the Stateflow chart outputs a value that indicates whether 0, 1, or 2 fans are running. The Simulink subsystem uses this value as an index into a multiport switch, which outputs a cooling activity value, as described in A Look at the Physical Plant.
Properties of Output. The properties of the airflow output are as follows:
| Property | Value |
|---|---|
| Name | airflow |
| Scope | Output |
| Data type | 8-bit unsigned integer (uint8) (The values can be only 0, 1, or 2.) |
| Port | 1 |
| Watch in debugger | Enable |
To begin building your Stateflow chart, you will add a Stateflow block to a partially built Simulink model called sf_aircontrol_exercise, which contains the Physical Plant subsystem, described in A Look at the Physical Plant.
To add a Stateflow block to an existing Simulink model:
Open the Simulink model by typing sf_aircontrol_exercise at the MATLAB command prompt.
The model opens on your desktop:

The model is incomplete because it does not include the Stateflow chart that you will build as you work through the exercises in this guide. Instead, the model contains several nonfunctional blocks: the Terminator, Inport, and Annotation blocks.
Delete the nonfunctional blocks and their connectors.
Your model should now look like this:

Save the model as Stage1Interface:
Create a new local folder for storing your working model.
In the Simulink model window, select File > Save As.
Navigate to the new folder.
Enter Stage1Interface.mdl as the file name.
Leave the default type as Simulink Models (*.mdl).
Click Save.
On the toolbar of the Simulink model, click the Library Browser icon:
![]()
The Simulink Library Browser opens on your desktop:

Add the Stateflow block to the Simulink model:
In the left scroll pane of the Library Browser, select Stateflow.
Drag the first block, called Chart, into your model.
The model should now look like this:

Click the label Chart under the Stateflow block and rename it Air Controller.
Shortcut for adding a Stateflow block to a new Simulink model
Inputs and outputs are data elements in a Stateflow chart that interact with the parent Simulink model. To define inputs and outputs for your chart, follow these steps:
Double-click the Air Controller block in the Simulink model Stage1Interface to open the Stateflow chart.
The Stateflow Editor opens on your desktop:

Add a data element to hold the value of the temperature input from the Simulink model:
From the Add menu, select Data > Input from Simulink.
The Data properties dialog box opens on your desktop with the General tab selected:

The default values in the dialog box depend on the scope — in this case, a data input.
In the Name field, change the name of the data element to temp.
Leave the following fields at their default values in the General tab because they meet the design requirements:
| Field | Default Value | What It Means |
|---|---|---|
| Scope | Input | Input from Simulink model. The data element gets its value from the Simulink signal on the same input port. |
| Size | -1 | The data element inherits its size from the Simulink signal on the same port. |
| Complexity | Off | The data element does not contain any complex values. |
| Type | Inherit: Same as Simulink | The data element inherits its data type from the Simulink signal on the same output port. |
In the General tab, select the Watch in debugger check box.
Enabling Watch in debugger lets you examine the value of temp during breakpoints in simulation. You will try this in Simulating the Chart.
Click OK to apply the changes and close the dialog box.
Add a data element to hold the value of the airflow output from the Air Controller chart:
From the Add menu, select Data > Output to Simulink.
The Data properties dialog box opens on your desktop, this time with different default values, associated with the scope Output:

In the Name field of the Data properties dialog box, change the name of the data element to airflow.
In the Type field, select uint8 (8-bit unsigned integer) from the submenu.
Look at the Initial value field.
The initial value is a blank expression, which indicates a default value of zero, based on the data type. This value is consistent with the model design, which specifies that no fans are running when the chart wakes up for the first time.
Make the following changes for other properties in the General tab:
| Property | What to Specify |
|---|---|
| Limit range | Enter 0 for Minimum and 2 for Maximum. |
| Watch in debugger | Select the check box to enable this option. |
Click OK to apply the changes and close the dialog box.
Go back to the Simulink model by clicking the up-arrow button in the Stateflow Editor toolbar:

Notice that the input temp and output airflow have been added to the Stateflow block:

Save Stage1Interface.
Tip There are several ways to add data objects to Stateflow charts. You used the Stateflow Editor, which lets you add data elements to the Stateflow chart that is open and has focus. However, to add data objects not just to a chart, but anywhere in the Stateflow design hierarchy, you can use a tool called the Model Explorer. This tool also lets you view and modify the data objects you have already added to a chart. For more information, see Stateflow Hierarchy of Objects and Adding Data Using the Model Explorer in the Stateflow User's Guide. You can also add data objects programmatically using the Stateflow API, as described in Creating Stateflow Objects in the Stateflow API Guide. |
Now that you have defined the inputs and outputs for the Stateflow Air Controller block, you need to connect them to the corresponding signals of the Simulink Physical Plant subsystem. Follow these steps:
In the model Stage1Interface, connect the output airflow from Air Controller to the corresponding input in Physical Plant:
Place your pointer over the output port for airflow on the right side of the Air Controller block.
The pointer changes in shape to crosshairs.
Hold down the left mouse button and move the pointer to the input port for airflow on the left side of the Physical Plant block.
Release the mouse.
The connection should look something like this:

Connect the output temp from the Physical Plant to the corresponding input in Air Controller by drawing a branch line from the line that connects temp to the Scope:
Place your pointer on the line where you want the branch line to start.
While holding down the Ctrl key, press and hold down the left mouse button.
Drag your pointer to the input port for temp on the left side of the Air Controller block.
Release the mouse button and the Ctrl key.
Reposition the connection so that it looks like this:

Save Stage1Interface.
Where to go next. Now you are ready to model the operating modes with states. See Implementing the States to Represent Operating Modes.
![]() | Defining the Interface to the Simulink Model | Defining the States for Modeling Each Mode of Operation | ![]() |

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 |