Documentation

Elements of an Enumerated Data Type Definition

The elements of an enumerated data type definition appear as follows:

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

  methods (Static = true)
      function retVal = getDescription()
      % GETDESCRIPTION Specifies a string to describe this enumerated type.
      retVal = 'This defines an enumerated type for colors';
    end

    function retVal = 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;
    end

    function retVal = 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';
    end

     function retVal = 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';
    end

    function retVal = addClassNameToEnumNames()
      % ADDCLASSNAMETOENUMNAMES Specifies whether to add the class name
      % as a prefix to enumeration member names in generated code.
      % Return true or false.
      % If you do not define this method, no prefix is added.
      retVal = true;
    end %function
  end %methods
end %classdef

The data type definition consists of three sections of code.

Section of CodeRequired?PurposeReference
classdef

Yes

Gives the name of the enumerated data type

Define an Enumerated Data Type in a File

enumeration

Yes

Lists the enumerated values that the data type allows

Define an Enumerated Data Type in a File

methods

No

Provides methods that customize the data type:

  • getDefaultValue

    Specifies a default enumerated value other than the first one in the list of allowed values

  • getDescription

    Specifies a description of the data type for Simulink® Coder™ generated code

  • getHeaderFile

    Specifies custom header file that contains the enumerated type definition in Simulink Coder generated code

  • getDataScope

    Enables exporting or importing the enumerated type definition to or from a header file in Simulink Coder generated code

  • addClassNameToEnumNames

    Prevents name conflicts with identifiers in Simulink Coder generated code, and enhances readability

Use Enumerated Data in Simulink Models

In the example, the methods section of code customizes the data type as follows:

  • 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 Simulink Coder generated code

  • Imports the definition of the data type from a custom header file to prevent Simulink Coder from generating the definition.

  • Adds the name of the data type as a prefix to each enumeration member name in Simulink Coder generated code

Was this topic helpful?