Customize Enumerated Types in Generated Code

For code generation, to customize an enumeration, in the static methods section of the class definition, include customized versions of the methods listed in this table.

MethodDescriptionDefault Value Returned or Specified When to Use

getDefaultValue

Returns the default enumerated value.

First value in the enumeration class definition.

For a default value that is different than the first enumeration value, provide a getDefaultValue method that returns the default value that you want. See Specify a Default Enumeration Value.

getHeaderFile

Specifies the file that defines an externally defined enumerated type.

''

To use an externally defined enumerated type, provide a getHeaderFile method that returns the path to the header file that defines the type. In this case, the code generator does not produce the class definition. See Specify a Header File

addClassNameToEnumNames

Specifies whether the class name becomes a prefix in the generated code.

false — prefix is not used.

If you want the class name to become a prefix in the generated code, set the return value of the addClassNameToEnumNames method to true. See Include Class Name Prefix in Generated Enumerated Type Value Names.

Specify a Default Enumeration Value

If the value of a variable that is cast to an enumerated type does not match one of the enumerated type values:

  • Generated MEX reports an error.

  • Generated C/C++ code replaces the value of the variable with the enumerated type default value.

Unless you specify otherwise, the default value for an enumerated type is the first value in the enumeration class definition. To specify a different default value, add your own getDefaultValue method to the methods section. In this example, the first enumeration member value is LEDcolor.GREEN, but the getDefaultValue method returns LEDcolor.RED:

classdef LEDcolor < int32
    enumeration
        GREEN(1),
        RED(2)
    end
    
    methods (Static)
        function y = getDefaultValue()
            y = LEDcolor.RED;
        end
    end
end

Specify a Header File

To specify that an enumerated type is defined in an external file, provide a customized getHeaderFile method. This example specifies that LEDcolor is defined in the external file my_LEDcolor.h.

classdef LEDcolor < int32
    enumeration
        GREEN(1),
        RED(2)
    end

    methods(Static)
      function y=getHeaderFile()
        y='my_LEDcolor.h';
      end
    end
end

You must provide my_LEDcolor.h. For example:

enum LEDcolor
{
    GREEN = 1,
    RED
};
typedef enum LEDcolor LEDcolor;

Include Class Name Prefix in Generated Enumerated Type Value Names

By default, the generated enumerated type value name does not include the class name prefix. For example:

enum LEDcolor
{
    GREEN = 1,
    RED
};

typedef enum LEDcolor LEDcolor;

To include the class name prefix, provide an addClassNameToEnumNames method that returns true. For example:

classdef LEDcolor < int32
    enumeration
        GREEN(1),
        RED(2)
    end

    methods(Static)
      function y = addClassNameToEnumNames()
        y=true;
      end
    end
end

In the generated type definition, the enumerated value names include the class prefix LEDcolor.

enum LEDcolor
{
    LEDcolor_GREEN = 1,
    LEDcolor_RED
};

typedef enum LEDcolor LEDcolor;

Related Topics