Documentation Center

  • Trial Software
  • Product Updates

Define Enumerated Data for Code Generation

Follow these steps to define enumerated data for code generation from MATLAB® algorithms:

  1. Create a class definition file.

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

  2. Enter the class definition as follows:

    classdef(Enumeration) EnumTypeName < int32

    For example, the following code defines an enumerated type called sysMode:

    classdef(Enumeration) sysMode < int32
        ...
    end

    EnumTypeName is a case-sensitive string that must be unique among data type names and workspace variable names. It must inherit from the built-in type int32.

  3. Define enumerated values in an enumeration section as follows:

    classdef(Enumeration) EnumTypeName < int32
      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, but can also appear in other enumerated types. The underlying integers need not be either consecutive or ordered, nor must they be unique within the type 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.

    To add a folder to the MATLAB search path, type addpath pathname at the MATLAB command prompt. For more information, see What Is the MATLAB Search Path?, addpath, and savepath.

For examples of enumerated data type definitions, see Define Enumerated Data for Code Generation.

Naming Enumerated Types for Code Generation

You must use a unique name for each enumerated data type. The name of an enumerated data type cannot match the name of a toolbox function supported for code generation, or another data type or a variable in the MATLAB base workspace. Otherwise, a name conflict occurs.

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?