Products & Services Solutions Academia Support User Community Company

Learn more about Stateflow   

How to Define Enumerated Data in a Stateflow Chart

Tasks for Defining Enumerated Data in a Chart

To use 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. For details, see Object-Oriented Programming in the MATLAB documentation.

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

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

Defining an Enumerated Data Type in a File

To define an enumerated data type:

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

    In the MATLAB Command Window, select File > 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 Overriding Default Methods (Optional) in the Simulink User's Guide.

  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.

Adding Enumerated Data to a Chart

To add enumerated data to a chart:

  1. In the Stateflow Editor, select Add > Data and choose a scope other than Constant.

    The Data properties dialog box appears.

  2. In the General pane, 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 Defining an Enumerated Data Type in a File.)

    4. Click Apply.

  3. (Optional) In the Value Attributes pane, 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 Stateflow Chart.)

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

    2. Click OK.

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2010- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS