Documentation Center

  • Trial Software
  • Product Updates

Define Enumerated Data in a Chart

Tasks for Defining Enumerated Data in a Chart

  1. Define an enumerated data type in a file on the MATLAB® path.

    This data type is a MATLAB class definition.

      Note:   For each enumerated type, you must create a new file.

  2. Add data of the enumerated type to a chart.

Define an Enumerated Data Type in a File

  1. Create a new file to store the data type definition.

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

  2. Define enumerated values in an enumeration section.

    classdef(Enumeration) EnumTypeName < Simulink.IntEnumType
      enumeration
        EnumName(N)
        ...
      end
    end

    EnumTypeName is a case-sensitive string that must be unique among data type names and workspace variable names. An enumerated type can define any number of values. Each enumerated value consists of a string EnumName and an integer N. Each EnumName must be unique within its type, but can also appear in other enumerated types.

    For example, you can enter the following lines in the MATLAB Editor:

    classdef(Enumeration) BasicColors < Simulink.IntEnumType
      enumeration
        Red(0)
        Yellow(1)
        Green(2)
      end
    end

    The classdef section defines an integer-based enumerated data type with the name BasicColors and derives it from the built-in type Simulink.IntEnumType. The enumeration section is the set of values that this data type allows. The default value is the first one in the list, unless you specify otherwise in the next step.

  3. (Optional) Customize the data type using a methods section.

    For details, see Elements of an Enumerated Data Type Definition or Customize Simulink Enumeration in the Simulink documentation.

  4. Save this file on the MATLAB path.

    The name of your file must match exactly the name of your data type. For example, the data type BasicColors must reside in a file named BasicColors.m.

      Tip   To add a folder to the MATLAB search path, type addpath pathname at the command prompt.

Add Enumerated Data to a Chart

  1. Add data to your chart and select a scope other than Constant.

  2. In the General pane of the Data properties dialog box, enter a name and data type for the enumerated data.

    1. In the Name field, enter a name.

    2. In the Type field, select Enum: <class name>.

        Note:   The Complexity field disappears when you select Enum: <class name> because enumerated data does not support complex values.

    3. Replace <class name> with the name of the data type that you defined in a file on the MATLAB path.

      For example, you can enter Enum: BasicColors in the Type field. (See Define an Enumerated Data Type in a File.)

    4. Click Apply.

  3. (Optional) Enter an initial value for the enumerated data.

    1. In the Initial value field, enter a prefixed identifier that refers to an enumerated value for this data type. (For details, see Rules for Using Enumerated Data in a Chart.)

      For example, BasicColors.Red is an identifier that uses prefixed notation. (See Prefixed Notation for Enumerated Values.)

    2. Click OK.

Was this topic helpful?