Reference Values by Name by Using Enumerated Data - MATLAB & Simulink (original) (raw)

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.

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 Value Name Integer Value
Red(0) Red 0
Yellow(1) Yellow 1
Green(2) Green 2

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, andCode) that contain the enumerated name Red. By using prefixed notation, you can distinguish Colors.Red fromTemp.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 nameRed 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 asColors.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:

Use enumerated data as arguments for:

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

See Also

Topics