Main Content

Best Practices for 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. For more information, see Reference Values by Name by Using Enumerated Data.

Guidelines for Defining Enumerated Data Types

Use Unique Name for Each Enumerated Type

To avoid name conflicts, the name of an enumerated data type cannot match the name of:

  • Another data type

  • A data object in the Stateflow chart

  • A variable in the MATLAB® base workspace

Use Same Name for Enumerated Type and Class Definition File

To enable resolution of enumerated data types for Simulink models, the name of the MATLAB file that contains the type definition must match the name of the data type.

Apply Changes in Enumerated Type Definition

When you update an enumerated data type definition for an open model, the changes do not take effect immediately. To see the effects of updating a data type definition:

  1. Save and close the model.

  2. Delete all instances of the data type from the MATLAB base workspace. To find these instances, type whos at the command prompt.

  3. Open the model and start simulation or generate code by using Simulink Coder™.

Guidelines for Referencing Enumerated Data

Ensure Unique Name Resolution for Nonprefixed Identifiers

If you use nonprefixed identifiers to refer to enumerated values in a chart, ensure that each enumerated name belongs to a unique enumerated data type.

Use Unique Identifiers for Enumerated Values

If an enumerated value uses the same identifier as a data object or a bus field, the chart does not resolve the identifier correctly. For example, this diagram shows the stages in which a chart tries to resolve the identifier Colors.Red.

Flow chart that shows the steps for resolving the identifier Colors.Red.

Set Initial Values of Enumerated Data by Using Prefixed Identifiers

If you choose to set an initial value for enumerated data, you must use a prefixed identifier in the Initial value field of the Property Inspector. For example, BasicColors.Red is a valid identifier, but Red is not. The initial value must evaluate to a valid MATLAB expression.

Enhance Readability of Generated Code by Using Prefixed Identifiers

If you add prefixes to enumerated names in the generated code, you enhance readability and avoid name conflicts with global symbols. For details, see Use Enumerated Data in Generated Code (Simulink Coder).

Guidelines and Limitations for Enumerated Data

Do Not Enter Minimum or Maximum Values for Enumerated Data

For enumerated data, leave the Minimum and Maximum properties empty. The chart ignores any values that you enter for these properties.

Whether these fields appear in the Property Inspector depends on which Type field option you use to define enumerated data.

Type Field OptionAppearance of the Minimum and Maximum Fields
Enum: <class name>Not available
<data type expression> or Inherit from SimulinkAvailable

Do Not Assign Enumerated Values to Constant Data

Because enumerated values are constants, assigning these values to constant data is redundant and unnecessary. If you try to assign enumerated values to constant data, an error appears.

Do Not Use ml Namespace Operator to Access Enumerated Data

The ml operator does not support enumerated data.

Related Topics