Skip to Main Content Skip to Search
Product Documentation

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

Defining an Enumerated Data Type in a File

enumeration

Yes

Lists the enumerated values that the data type allows

Defining 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

Enumerations and Modeling in the Simulink User's Guide

Enumerations in the Simulink Coder User's Guide

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

  


Free Stateflow Interactive Kit

Learn how engineers use Stateflow to model state machines in their Simulink models.


Get free kit

Trials Available

Try the latest version of Stateflow.


Get trial software
 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS