Enumerated Types Supported in MATLAB Function Blocks

An enumerated type is a user-defined type whose values belong to a predefined set of enumerated values. Each enumerated value consists of a name and an underlying numeric value.

You define an enumerated data type in an enumeration class definition file. For use in a MATLAB Function block, you must base the class on Simulink.IntEnumType, int8, uint8, int16, uint16, or int32. For example:

classdef(Enumeration) PrimaryColors < Simulink.IntEnumType
    enumeration
        Red(1),
        Blue(2),
        Yellow(4)
    end
end

In this example, the statement classdef(Enumeration) PrimaryColors < Simulink.IntEnumType means that the enumerated type PrimaryColors is based on the Simulink.IntEnumType class. PrimaryColors inherits the characteristics of the Simulink.IntEnumType class. It also defines its own unique characteristics. For example, PrimaryColors is restricted to three enumerated values.

Enumerated ValueEnumerated NameUnderlying Numeric Value
Red(1)Red1
Blue(2)Blue2
Yellow(4)Yellow4

If the enumerated type inherits from a base type supported for a MATLAB Function block, you can exchange enumerated data between MATLAB Function blocks and other Simulink® blocks in a model.

Enumeration Class Base Types in MATLAB Function Block

For MATLAB Function blocks, you must base an enumerated type on the Simulink.IntEnumType class or one of the following built-in MATLAB® integer data types:

  • int8

  • uint8

  • int16

  • uint16

  • int32

You can use the base type to control the size of an enumerated type in generated C/C++ code. You can:

  • Represent an enumerated type as a fixed-size integer that is portable to different targets.

  • Reduce memory usage.

  • Interface to legacy code.

  • Match company standards.

The base type determines the representation of the enumerated type in generated C/C++ code.

C Code Representation for Simulink.IntEnumType Base Type

If the base type is Simulink.IntEnumType , the code generation software generates a C enumeration type. Consider the following MATLAB enumerated type definition:

classdef(Enumeration) LEDcolor < Simulink.IntEnumType
    enumeration
        GREEN(1),
        RED(2)
    end
end

This enumerated type definition results in the following C code:

typedef enum {
  GREEN = 1,                          
  RED
} LEDcolor;

C Code Representation for Built-In Integer Base Types

For built-in integer base types, the code generation software generates a typedef statement for the enumerated type and #define statements for the enumerated values. Consider the following MATLAB enumerated type definition:

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

This enumerated type definition results in the following C code:

typedef int16_T LEDcolor;

#define GREEN                          ((LEDcolor)1)             
#define RED                            ((LEDcolor)2)
Was this topic helpful?