| Contents | Index |
| On this page… |
|---|
The goal of this tutorial is to build a chart that uses enumerated values in assignment statements.

To build the chart, follow these steps.
You can add states and transitions to the chart as follows.
Type sfnew at the command prompt to create a new model with a chart inside.
In the chart, add states A and B to the chart.

Add transitions between states A and B.

Add a default transition to state A.

To define an enumerated data type for the chart:
Create a new file to store the data type definition.
In the MATLAB Command Window, select File > New > Enumeration.
Enter these lines in the MATLAB Editor:
classdef(Enumeration) TrafficColors < Simulink.IntEnumType
enumeration
RED(0)
GREEN(10)
end
end The classdef section defines an integer-based enumerated data type named TrafficColors that is derived from the built-in type Simulink.IntEnumType. The enumeration section is the set of enumerated values that this data type allows. Each enumerated name is followed by the underlying numeric value.
Save your file as TrafficColors.m in a folder on the MATLAB search path.
The name of your file must match exactly the name of your data type. Therefore, you must use TrafficColors.m as the name of your file.
To add the enumerated data color to the chart:
In the Stateflow Editor, select Add > Data > Output to Simulink.
The Data properties dialog box appears.
In the General pane, enter color in the Name field.
In the Type field, select Enum: <class name>.
Replace <class name> with TrafficColors, the name of the data type that you defined in a file in Defining an Enumerated Data Type for the Chart.
Click OK.
To add the integer data y to the chart:
In the Stateflow Editor, select Add > Data > Output to Simulink.
The Data properties dialog box appears.
In the General pane, enter y in the Name field.
In the Type field, select uint8.
Click OK.
To view results for simulation, follow these steps.
You can add two scopes to your model as follows.
Open the Simulink Library Browser.
In the Simulink/Sinks library, select the Scope block.
Add two scopes to your model as shown.

You can set a discrete sample time for simulation using a fixed-step solver. (For details, see Solvers in the Simulink User's Guide.)
Open the Configuration Parameters dialog box (for example, by selecting Simulation > Configuration Parameters in the Stateflow Editor).
In the Solver pane, select Fixed-step in the Type field.
Select Discrete (no continuous states) in the Solver field.
Enter 0.2 in the Fixed-step size (fundamental sample time) field.
Click OK.
Open the Scope blocks. When you simulate the model, you get the following results:


During simulation, the chart works as follows.
After the chart wakes up, state A is entered.
State A executes the entry action by assigning the value RED to the enumerated data color.
The data y increments once per time step (every 0.2 seconds) until the condition [y > 6] is true.
The chart takes the transition from state A to state B.
After the transition from state A occurs, state B is entered.
State B executes the entry action by assigning the value GREEN to the enumerated data color.
The data y decrements once per time step (every 0.2 seconds) until the condition [y < 3] is true.
The chart takes the transition from state B to state A.
States A and B take turns executing until the simulation ends.
![]() | CD Player Model That Uses Enumerated Data | Modeling Continuous-Time Systems in Stateflow Charts | ![]() |

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 |