Documentation Center

  • Trial Software
  • Product Updates

Elements of an Enumerated Data Type Definition

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

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

  methods (Static = true)
    function retVal = getDefaultValue()
      % GETDEFAULTVALUE  Returns the default enumerated value.
      %   This value must be an instance of the enumerated type.
      %   Used by Simulink when an instance of this class is 
      %   needed but the value is not known (e.g., initializing 
      %   ground values or casting an invalid numeric value to 
      %   an enumerated type). If this method is not defined, 
      %   the first value is used.
      retVal = BasicColors.Green;
    end

    function retVal = getDescription()
      % GETDESCRIPTION  Optional string to describe data type.
      retVal = 'This defines an enumerated type for colors';
    end

    function retVal = getHeaderFile()
      % GETHEADERFILE  File where type is defined for generated 
      %   code. If specified, this file is #included as needed 
      %   in the code. Otherwise, the type is written out in 
      %   the generated code.
      retVal = 'imported_enum_type.h';
    end

    function retVal = addClassNameToEnumNames()
      % ADDCLASSNAMETOENUMNAMES  Specify if class name is added
      %   as a prefix to enumerated names in the generated code.
      %   By default we do not add the prefix.
      retVal = true;
    end
  end
end

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

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

  • getHeaderFile

    Enables importing of custom header files that contain enumerated type definitions for 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

  • Uses a custom header file to prevent the data type from being written out in Simulink Coder generated code

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

Was this topic helpful?