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.
Method | Description | Default Value Returned or Specified | When to Use |
---|---|---|---|
| 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 |
| Specifies the file that defines an externally defined enumerated type. |
| To use an externally defined enumerated type, provide
a |
| Specifies whether the class name becomes a prefix in the generated code. |
| If you want the class name to become a prefix in the generated code, set the return
value of the Note When generating C++11 enumeration classes, the code generator ignores this static method. |
generateEnumClass | Specifies whether to generate C++11 enumeration classes | true — enumeration classes are generated in
C++11 code | When generating C++11 code, to instruct the code generator to produce
ordinary C enumeration for a particular MATLAB® enumeration, set the return value of
generateEnumClass method to
false . See Generate C++11 Code Containing Ordinary C Enumeration. |
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;
If you place the MATLAB enumeration LEDcolor
inside the package
pkg
and generate C++ code, code generation preserves the
name of this enumeration and places it inside the namespace
pkg
in the generated code. Therefore, in the header file
that you provide, you must define this enumeration inside the namespace
pkg
.
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;
Generate C++11 Code Containing Ordinary C Enumeration
When you generate C++11 code, your MATLAB enumeration class is converted to a C++11 enumeration class. For example:
enum class MyEnumClass16 : short { Orange = 0, // Default value Yellow, Pink };
To generate an ordinary C enumeration instead, provide a
generateEnumClass
method that returns
false
. For example:
classdef MyEnumClass16 < int16 enumeration Orange(0), Yellow(1), Pink(2) end % particular enum opting out methods(Static) function y = generateEnumClass() y = false; end end end
Now the generated C++11 code contains an ordinary C enumeration.
enum MyEnumClass16 : short { Orange = 0, // Default value Yellow, Pink };