Main Content

Define Enumerated Data Types

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, seeReference Values by Name by Using Enumerated Data.

Before you can add enumerated data to a Stateflow chart, you must define an enumerated data type in a MATLAB®class definition file. Create a different file for each enumerated type.

Elements of an Enumerated Data Type Definition

The enumerated data type definition consists of three sections of code.

Section of Code Required? Purpose
classdef

Yes

Provides the name of the enumerated data type

enumeration

Yes

Lists the enumerated values that the data type allows

methods

No

Provides methods that customize the data type

Define an Enumerated Data Type

  1. Open a new file in which to store the data type definition. From theHometab on the MATLAB toolstrip, selectNew>Class.

  2. Complete theclassdefsection of the definition.

    classdefBasicColors < Simulink.IntEnumType...end

    Theclassdefsection defines an enumerated data type with the nameBasicColors. Stateflow derives the data type from the built-in typeSimulink.IntEnumType. The enumerated data type name must be unique among data type names and workspace variable names.

  3. Define enumerated values in anenumerationsection.

    classdefBasicColors < Simulink.IntEnumTypeenumerationRed(0) Yellow(1) Green(2)endend

    An enumerated type can define any number of values. Theenumerationsection lists the set of enumerated values that this data type allows. Each enumerated value consists of a name and an underlying integer value. Each name must be unique within its type, but can also appear in other enumerated types. The default value is the first one in the list, unless you specify otherwise in themethodssection of the definition.

  4. (Optional) Customize the data type by using amethodssection. The section can contain these methods:

    • getDefaultValuespecifies a default enumerated value other than the first one in the list of allowed values.

    • getDescriptionspecifies a description of the data type for code generated bySimulink Coder™.

    • getHeaderFilespecifies custom header file that contains the enumerated type definition in code generated bySimulink Coder.

    • getDataScopeenables exporting or importing the enumerated type definition to or from a header file in code generated bySimulink Coder.

    • addClassNameToEnumNamesenhances readability and prevents name conflicts with identifiers in code generated bySimulink Coder.

    For example, this MATLAB file presents a customized definition for the enumerated data typeBasicColorsthat:

    • Specifies that the default enumerated value is the last one in the list of allowed values.

    • Includes a short description of the data type for code generated bySimulink Coder.

    • Imports the definition of the data type from a custom header file to preventSimulink Coderfrom generating the definition.

    • 增加了数据类型的名称作为前缀enumeration member name in code generated bySimulink Coder.

    classdefBasicColors < Simulink.IntEnumTypeenumerationRed(0) Yellow(1) Green(2)endmethods(Static = true)functionretVal = getDefaultValue()% GETDEFAULTVALUE Specifies the default enumeration member.% Return a valid member of this enumeration class to specify the default.% If you do not define this method, Simulink uses the first member.retVal = BasicColors.Green;endfunctionretVal = getDescription()% GETDESCRIPTION Specifies a string to describe this enumerated type.retVal ='This defines an enumerated type for colors';endfunctionretVal = getHeaderFile()% GETHEADERFILE Specifies the file that defines this type in generated code.% The method getDataScope determines the significance of the specified file.retVal ='imported_enum_type.h';endfunctionretVal = getDataScope()% GETDATASCOPE Specifies whether generated code imports or exports this type.% Return one of these strings:% 'Auto': define type in model_types.h, or import if header file specified% 'Exported': define type in a generated header file% 'Imported': import type definition from specified header file% If you do not define this method, DataScope is 'Auto' by default.retVal ='Imported';endfunctionretVal = addClassNameToEnumNames()% ADDCLASSNAMETOENUMNAMES Specifies whether to add the class name% as a prefix to enumeration member names in generated code.% Return true or false.%如果您没有定义这个方法,没有前缀是一个dded.retVal = true;end% functionend% methodsend% classdef

  5. Save the file on the MATLAB path. The name of the file must match exactly the name of the data type. For example, the definition for the data typeBasicColorsmust reside in a file namedBasicColors.m.

    Tip

    To add a folder to the MATLAB search path, typeaddpathpathnameat the command prompt.

Specify Data Type in the Property Inspector

When you add enumerated data to your chart, specify its type in theProperty Inspector.

  1. In theTypefield, selectEnum: .

  2. Replacewith the name of the data type. For example, you can enterEnum: BasicColorsin theTypefield.

  3. (Optional) Enter an initial value for the enumerated data by using a prefixed identifier. The initial value must evaluate to a valid MATLAB expression. For more information on prefixed and nonprefixed identifiers, seeNotation for Enumerated Values.

Related Topics