Assign Enumerated Values in a Chart - MATLAB & Simulink (original) (raw)

Main Content

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

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

Execution of State A

Execution of State B

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 theHome tab on the MATLAB toolstrip, select > .
  2. In the MATLAB Editor, enter:
    classdef TrafficColors < Simulink.IntEnumType
    enumeration
    RED(0)
    GREEN(10)
    end
    end
    Theclassdef 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 theResolve undefined symbols icon . The Stateflow Editor assigns an appropriate scope to each symbol in the chart.
    Symbol Scope
    color Output Data
    y Local Data

| GREEN | Parameter Data |
| RED | Parameter Data | 2. To specify color as enumerated data, in the Property Inspector:

  1. To set the scope and type of y, in the Property Inspector:
    • In the Scope field, selectOutput.
    • In the Type field, selectuint8.
    • Under Logging, select the Log signal data check box.
  2. In the Symbols pane, delete the symbolsGREEN 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 signalscolor 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

Simulation Data Inspector (Simulink)

Topics