Products & Services Solutions Academia Support User Community Company

Learn more about Simulink   

Using Enumerated Data in a Simulink Model

Simulating with Enumerated Types

An enumerated data type definition resembles a list of named constants that equate to integer values, but the values in the enumerated type appear in a MATLAB class definition, and the integers are used only for internal operations. The following code defines an enumerated class (or type) named BasicColors whose enumerated values are Red, Yellow, and Blue, with Blue as the default value:

classdef(Enumeration) BasicColors < Simulink.IntEnumType
  enumeration
    Red(0)
    Yellow(1)
    Blue(2)
  end
  methods (Static = true)
    function retVal = getDefaultValue()
      retVal = BasicColors.Blue;
    end
  end
end

This is an ordinary MATLAB class definition. The first line defines an integer-based enumerated data type that is derived from the built-in class Simulink.IntEnumType. The enumeration section specifies the enumerated values that comprise the type:

Enumerated ValueEnumerated NameUnderlying Integer Value
Red(0)Red0
Yellow(1)Yellow1
Blue(2)Blue2

The methods section is optional, and contains a getDefaultValue method that defines the default value for the type to be BasicColors.Blue. If that method did not appear, the default value would be Red, because that is the lexically first value in the enumerated class definition.

Once this class definition is known to MATLAB, you can use the enumerated type in Simulink and Stateflow models. The following model uses the enumerated type defined above:

The output of the model looks like this:

The Data Type Conversion block OrigToInt specifies an Output data type of int32 and Integer rounding mode: Floor, so the block converts the sine wave, which is shown in the top graph of the Scope display, to a cycle of integers: 1, 2, 1, 0, 1, 2, 1. The Data Type Conversion block IntToColor uses these values to select colors from the enumerated type BasicColors by referencing their underlying integers.

The result is a cycle of colors: Yellow, Blue, Yellow, Red, Yellow, Blue, Yellow, as shown in the second graph. The Constant block outputs Yellow, which appears in the second graph as a straight line. The Relational Operator block compares the constant Yellow to each value in the cycle of colors. It outputs 1 (true) when Yellow is less than the current color, and 0 (false) otherwise, as shown in the third graph.

The sort order used by the comparison is the order of the underlying integers of the compared values, not the order in which the enumerated values appear lexically in the enumerated class definition. In this case the two orders are the same, but they need not be. See Enumerated Values in Computation for more information.

Referencing an Enumerated Type

Once you have defined an enumerated type, you can use it much like any other data type. Because an enumerated type is a class rather than an instance, you must prefix the keyword Enum: to the type name when you specify an enumerated data type. You can type in Enum: followed by the name of an enumerated type, or select Enum: <class name> from the Output data type pulldown, then replace <class name>. To use the Data Type Assistant, set the Mode to Enumerated, then enter the name of the enumerated type. For example, in the previous model the Data Type Conversion block IntToColor, which outputs a signal of type BasicColors, has the following output signal specification:

Prefixing Enum: has the same effect as prefixing ? to a class name when working directly in MATLAB, as described in Obtaining Information About Classes with Meta-Classes. You can use either syntax in a Simulink model, but Enum: is preferable because it is more self-explanatory in the context of a graphical user interface. Only the ? syntax works in the MATLAB Command Window.

You cannot set a minimum or maximum value for a signal of an enumerated type, because the concepts of minimum and maximum are not relevant to the purpose of enumerated types. If you change the minimum or maximum for a signal of an enumerated type from the default value of [], an error occurs when you update the model.

Instantiating an Enumerated Type

To use a specific value from an enumerated type, you must instantiate the value. You can instantiate a numeric type in MATLAB or a Simulink model. The syntax is the same in either environment.

Instantiating an Enumerated Type in MATLAB

To instantiate an enumerated type in MATLAB, enter ClassName.EnumName in the MATLAB Command Window. The instance is created in the base workspace. For example, if BasicColors is defined as before, you can type:

>> bcy = BasicColors.Yellow

bcy = 

    Yellow

Tab Completion.   Tab completion works for enumerated types. For example, if you enter:

BasicColors.<tab>

MATLAB displays the elements and methods of BasicColors in alphabetical order:

You can double-click any element or method to insert it at the position where you pressed <tab>. See Completing Statements in the Command Window — Tab Completion for more information.

Casting Enumerated Types in MATLAB

In MATLAB, you can cast directly from an integer to an enumerated value:

>> bcb = BasicColors(2)

bcb = 

    Blue   

You can also cast from an enumerated value to its underlying integer:

>> bci = int32(bcb)

bci = 

    2   

In either case, MATLAB returns the result of the cast in a 1x1 array of the relevant type.

Instantiating an Enumerated Type in Simulink

To instantiate an enumerated type in a Simulink model, you can enter ClassName.EnumName as a value in a dialog box. For example, in the previous model the block Constant, which outputs the enumerated value Yellow, defines that value as follows:

You can enter any valid MATLAB expression that evaluates to an enumerated value. For example, you can enter BasicColors(2), or if you had previously executed bcy = BasicColors.Yellow in MATLAB, you could enter bcy.

If you create a Simulink.Parameter object of an enumerated type, you must specify the parameter value as a class instance, using a valid MATLAB expression such as ClassName.EnumName:

You cannot specify the parameter value directly as the numeric value of its underlying integer. Thus the following would fail even though the underlying integer of BasicColors.Yellow is 1:

Displaying Enumerated Values

Wherever possible, displaying an enumerated value displays its name, not its underlying integer. However, the underlying integers can affect enumerated value display in Scope blocks and Floating Scope blocks:

Enumerated Values with Non-Unique Integers

More than one enumerated value in a type can have the same underlying integer, as described in . When this occurs, displaying the value on a scope axis or in a Display block always shows the lexically first value in the enumerated class definition that has the shared underlying integer. For example:

Although the Constant block outputs True, both On and True have the same underlying integer, and On is defined first in the enumeration section, so the Display block shows On. Similarly, a Scope axis would show only On, never True, no matter which of the values is input to the Scope.

Enumerated Values in Computation

By design, the Simulink software prevents enumerated values from being used as numeric values in mathematical computation, even though an enumerated class is a subclass of the MATLAB int32 class. Thus an enumerated type does not function as a numeric type despite the existence of its underlying integers. For example, you can not input an enumerated signal directly to a Gain block.

You can use a Data Type Conversion block to convert in either direction between an integer type and an enumerated type, or between two enumerated types. Thus you can use a Data Type Conversion block to convert an enumerated signal to an integer signal (consisting of the underlying integers of the enumerated signal values) and input the resulting integer signal to a Gain block. See Casting Enumerated Signals for more information.

Enumerated types are intended to represent program states and to control program logic in Stateflow blocks and in Simulink blocks like the Relational Operator block and the Switch block. When a Simulink block compares enumerated values, the values compared must be of the same enumerated type. The block compares enumerated values based on their underlying integers, not their lexical order in the enumerated class definition.

When a block like the Switch block or Multiport Switch block selects among multiple data signals, and any data signal is of an enumerated type, all the data signals must be of that same enumerated type. When a block inputs both control and data signals, as Switch and Multiport Switch do, the control signal type need not match the data signal type.

Casting Enumerated Signals

You can use a Data Type Conversion block to cast an enumerated signal to a signal of any numeric type, provided that the underlying integers of all enumerated values input to the block are within the range of the numeric type. Otherwise, an error occurs during simulation.

You can use a Data Type Conversion block to cast a signal of any integer type to an enumerated signal, provided that every value input to the Data Type Conversion block is the underlying integer of some value in the enumerated type. Otherwise, an error occurs during simulation.

You cannot use a Data Type Conversion block to cast a numeric signal of any non-integer data type to an enumerated type. For example, the model used in Simulating with Enumerated Types needed two Data Conversion blocks to convert a sine wave to enumerated values:

The first block casts double to int32, and the second block casts int32 to BasicColors. You cannot cast a complex signal to an enumerated type regardless of the data types of its real and imaginary parts.

Casting Enumerated Block Parameters

You cannot cast a block parameter of any numeric data type to an enumerated data type. For example, suppose that a Constant block specified a Constant value of 2 and an Output data type of Enum: BasicColors:

An error would occur because the specifications implicitly cast a double value to an enumerated type. The error occurs even though the numeric value corresponds to one of the enumerated values in the enumerated type.

You cannot cast a block parameter of an enumerated data type to any other data type. For example, suppose that a Constant block specified a Constant value of BasicColors.Blue and an Output data type of int32:

An error would occur because the specifications implicitly cast an enumerated value to a numeric type. The error occurs even though the enumerated value's underlying integer is a valid int32.

  


Related Products & Applications

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

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