Main Content

Assign Enumerated Values in a Chart

To enhance the readability of a Stateflow® chart, use enumerated data. With enumerated data, you can:

  • Create a restricted set of values and refer to those values by name.

  • Group related values into separate data types.

  • Avoid defining a long list of constants.

Enumerated data is supported in Stateflow charts in Simulink® models. For more information, see Reference Values by Name by Using Enumerated Data.

Chart Behavior

This example shows how to build a chart that uses enumerated values to issue a status keyword.

Stateflow chart with states called A and B.

During simulation, the chart action alternates between states A and B.

Execution of State A

  • At the start of the simulation, 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.

Execution of 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 back to state A.

Build the Chart

Add States and Transitions to the Chart

  1. To create a Simulink model with an empty chart, at the MATLAB® command prompt, enter sfnew.

  2. In the empty chart, add states A and B. At the text prompt, enter the appropriate action statements.

  3. Add a default transition to state A and transitions between states A and B.

  4. Double-click each transition. At the text prompt, enter the appropriate condition.

Define an Enumerated Data Type for the Chart

  1. To create a file in which to store the data type definition, from the Home tab on the MATLAB toolstrip, select New > Class.

  2. In the MATLAB Editor, enter:

    classdef TrafficColors < Simulink.IntEnumType
      enumeration
        RED(0)
        GREEN(10)
      end
    end  
    The classdef section defines an integer-based enumerated data type named TrafficColors. The enumeration section contains the enumerated values that this data type allows followed by their underlying numeric value.

  3. Save your file as TrafficColors.m in a folder on the MATLAB search path.

Define Chart Data

  1. To resolve the undefined data, in the Symbols pane, click the Resolve undefined symbols icon . The Stateflow Editor assigns an appropriate scope to each symbol in the chart.

    SymbolScope
    colorOutput Data
    yLocal Data
    GREENParameter Data
    REDParameter Data

  2. To specify color as enumerated data, in the Property Inspector:

    • In the Type field, select Enum: <class name>. Replace <class name> with TrafficColors, the name of the data type that you defined previously.

    • Under Logging, select the Log signal data check box.

  3. To set the scope and type of y, in the Property Inspector:

    • In the Scope field, select Output.

    • In the Type field, select uint8.

    • Under Logging, select the Log signal data check box.

  4. In the Symbols pane, delete the symbols GREEN and RED. The Stateflow Editor incorrectly identified these symbols as parameters before you specified color as enumerated data.

View Logged Output

  1. Simulate the model.

  2. In the Simulation tab, under Review Results, select Data Inspector .

  3. In the Simulation Data Inspector (Simulink), in the Inspect pane, select the signals color and y. You can display the logged signals together or in separate axes. For more information, see Inspect Simulation Data (Simulink).

    Simulation Data Inspector showing simulation results for color and y.

  4. To access the logged data in the MATLAB workspace, call the signal logging object logsout. For example, at the command prompt, enter:

    losgout = out.logsout;
    colorLog = logsout.getElement("color");
    Tbl = table(colorLog.Values.Time,colorLog.Values.Data);
    Tbl.Properties.VariableNames = ["SimulationTime","Color"]
    Tbl =
    
      9×2 table
    
        SimulationTime    Color
        ______________    _____
    
               0          RED
             1.6          GREEN
             2.8          RED
               4          GREEN
             5.2          RED
             6.4          GREEN
             7.6          RED
             8.8          GREEN
              10          RED
    

See Also

(Simulink)

Related Topics