Main Content

Reference Values by Name by Using Enumerated Data

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.

Example of Enumerated Data

An enumerated data type is a finite collection of enumerated values consisting of a name and an underlying integer value. For example, this chart uses enumerated data to refer to a set of colors.

Chart that outputs enumerated data with values of Red, Yellow, and Green.

The enumerated data output is restricted to a finite set of values. You can refer to these values by their names: Red, Yellow, and Green.

Enumerated ValueNameInteger Value
Red(0)Red0
Yellow(1)Yellow1
Green(2)Green2

This MATLAB® file defines the enumerated data type BasicColors referenced by the chart.

classdef BasicColors < Simulink.IntEnumType
  enumeration
    Red(0)
    Yellow(1)
    Green(2)
  end
end

Computation with Enumerated Data

An enumerated data type does not function as a numeric type despite the existence of the underlying integer values. You cannot use enumerated values directly in a mathematical computation. You can use enumerated data to control chart behavior based on assignments and comparisons. To assign or compare enumerated data, use the operations listed in this table.

Example

Description

a = exp

Assignment of exp to a. exp must evaluate to an enumerated value.

a == b

Comparison, equality.

a != b

Comparison, inequality.

In a chart that uses C as the action language, you can compare enumerated data with different data types. Before the comparison, the chart casts the enumerated data to their underlying integer values.

Charts that use MATLAB as the action language cannot compare enumerated data with different data types.

Notation for Enumerated Values

To refer to an enumerated value, use prefixed or nonprefixed identifiers.

Prefixed Identifiers

To prevent name conflicts when referring to enumerated values in Stateflow charts, you can use prefixed identifiers of the form Type.Name. Type is an enumerated data type and Name is an enumerated value name. For example, suppose that you define three data types (Colors, Temp, and Code) that contain the enumerated name Red. By using prefixed notation, you can distinguish Colors.Red from Temp.Red and Code.Red.

Nonprefixed Identifiers

To minimize identifier length when referring to unique enumerated values, you can use nonprefixed enumerated value names. For example, suppose that the enumerated name Red belongs only to the data type Colors. You can then refer to this value with the nonprefixed identifier Red.

If your chart uses data types that contain identical enumerated names (such as Colors.Red and Temp.Red), use prefixed identifiers to prevent name conflicts.

Where to Use Enumerated Data

Use enumerated data at these levels of the Stateflow hierarchy:

  • Chart

  • Subchart

  • State

Use enumerated data as arguments for:

  • State actions

  • Condition and transition actions

  • Vector and matrix indexing

  • MATLAB functions

  • Graphical functions

  • Simulink functions

  • Truth Table blocks and truth table functions

If you have Simulink Coder™ installed, you can use enumerated data for simulation and code generation.

Related Topics