Assign Enumerated Values in a Chart

Goal of the Tutorial

The goal of this tutorial is to build a chart that uses enumerated values in assignment statements.

Build the Chart

To build the chart, follow these steps.

Add States and Transitions to the Chart

You can add states and transitions to the chart as follows.

  1. Type sfnew at the command prompt to create a new model with a chart inside.

  2. In the chart, add states A and B to the chart.

      Note:   You will define the data color and y in the sections that follow.

  3. Add transitions between states A and B.

  4. Add a default transition to state A.

Define an Enumerated Data Type for the Chart

To define an enumerated data type for the chart:

  1. Create a new file to store the data type definition.

    In the MATLAB® Command Window, select Home > New > Class.

  2. 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.

  3. 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.

      Tip   To add a folder to the MATLAB search path, type addpath pathname at the command prompt.

Add Enumerated Data to the Chart

To add the enumerated data color to the chart:

  1. In the Stateflow® Editor, select Chart > Add Inputs & Outputs > Data Output To Simulink.

    The Data properties dialog box appears.

  2. In the General pane, enter color in the Name field.

  3. In the Type field, select Enum: <class name>.

  4. Replace <class name> with TrafficColors, the name of the data type that you defined in a file in Define an Enumerated Data Type for the Chart.

  5. Click OK.

Add Integer Data to the Chart

To add the integer data y to the chart:

  1. In the Stateflow Editor, select Chart > Add Inputs & Outputs > Data Output To Simulink.

    The Data properties dialog box appears.

  2. In the General pane, enter y in the Name field.

  3. In the Type field, select uint8.

  4. Click OK.

View Results for Simulation

To view results for simulation, follow these steps.

Add Scopes to View Output

You can add two scopes to your model as follows.

  1. Open the Simulink® Library Browser.

  2. In the Simulink/Sinks library, select the Scope block.

  3. Add two scopes to your model as shown.

Set the Sample Time for Simulation

You can set a discrete sample time for simulation using a fixed-step solver. (For details, see Solvers in the Simulink documentation.)

  1. Open the Model Configuration Parameters dialog box.

  2. In the Solver pane, select Fixed-step in the Type field.

  3. Select Discrete (no continuous states) in the Solver field.

  4. Enter 0.2 in the Fixed-step size (fundamental sample time) field.

  5. Click OK.

Simulate the Model

Open the Scope blocks. When you simulate the model, you get the following results:

How the Chart Works

During simulation, the chart works as follows.

Stage 1: Execution of State A

  1. After the chart wakes up, state A is entered.

  2. State A executes the entry action by assigning the value RED to the enumerated data color.

  3. The data y increments once per time step (every 0.2 seconds) until the condition [y > 6] is true.

  4. The chart takes the transition from state A to state B.

Stage 2: Execution of State B

  1. After the transition from state A occurs, state B is entered.

  2. State B executes the entry action by assigning the value GREEN to the enumerated data color.

  3. The data y decrements once per time step (every 0.2 seconds) until the condition [y < 3] is true.

  4. The chart takes the transition from state B to state A.

Stage 3: Repeat of State Execution

States A and B take turns executing until the simulation ends.

Was this topic helpful?