Define Enumerated Data for Code Generation

To define enumerated data for code generation from MATLAB® algorithms:

  1. Create a class definition file.

    In the Command Window, select File > New > Class.

  2. Enter the class definition:

    classdef(Enumeration) EnumTypeName < BaseType

    EnumTypeName is a case-sensitive string that must be unique among data type names and workspace variable names. BaseType must be int8, uint8, int16, uint16, or int32.

    For example, the following code defines an enumerated type called sysMode that inherits from the built-in type int32:

    classdef(Enumeration) sysMode < int32
        ...
    end
  3. Define enumerated values in an enumeration section:

    classdef(Enumeration) EnumTypeName < BaseType
      enumeration
        EnumName(N)
        ...
      end
    end

    For example, the following code defines a set of two values for enumerated type sysMode:

    classdef(Enumeration) sysMode < int32
      enumeration
        OFF(0),
        ON(1)
      end
    end

    Each enumerated value consists of a string EnumName and an underlying integer N. Each EnumName must be unique within its type. If the enumerated value name does not include the class name prefix, EnumName must be unique across enumerated types. See Control Names of Enumerated Type Values in Generated Code.

    The underlying integers do not have to be consecutive or ordered, or unique within or across types.

  4. Save the file on the MATLAB path.

    The name of the file must match the name of the enumerated data type. The match is case sensitive.

Naming Enumerated Types for Code Generation

You must use a unique name for each enumerated data type. Do not use the name of:

  • A toolbox function supported for code generation.

  • Another data type.

  • A variable in the MATLAB base workspace.

For example, you cannot name an enumerated data type mode because MATLAB for code generation provides a toolbox function of the same name.

For a list of toolbox functions supported for code generation, see Functions and Objects Supported for C and C++ Code Generation — Alphabetical List.

Was this topic helpful?