Main Content

Named Values

Kinds of Predefined Names

MATLAB® supports two kinds of predefined names:

  • Constant properties

  • Enumerations

Constant Properties

Use constant properties when you want a collection of related constant values that can belong to different types (numeric values, character strings, and so on). Define properties with constant values by setting the property Constant attribute. Reference constant properties by name whenever you need access to that particular value.

See Define Class Properties with Constant Values for more information.


Use enumerations when you want to create a fixed set of names representing a single type of value. Use this new type in multiple places without redefining it for each class.

You can derive enumeration classes from other classes to inherit the operations of the superclass. For example, if you define an enumeration class that subclasses a MATLAB numeric class like double or int32, the enumeration class inherits all the mathematical and relational operations that MATLAB defines for those classes.

Using enumerations instead of character strings to represent a value, such as colors ('red'), can result in more readable code because:

  • You can compare enumeration members with == instead of using strcmp

  • Enumerations maintain type information, while char vectors do not. For example, passing a char vector 'red' to functions means that every function must interpret what 'red' means. If you define red as an enumeration, the actual value of 'red' can change (from [1 0 0] to [.93 .14 .14], for example) without updating every function that accepts colors, as you would if you defined the color as the char vector 'red'.

Define enumerations by creating an enumeration block in the class definition.

See Define Enumeration Classes for more information.

Techniques for Defining Enumerations

Enumerations enable you to define names that represent entities useful to your application, without using numeric values or character strings. All enumerations support equality and inequality operations. Therefore, switch, if, and several comparison functions like isequal and ismember work with enumeration members.

You can define enumeration classes in ways that are most useful to your application, as described in the following sections.

Simple Enumerated Names

Simple enumeration classes have no superclasses and no properties. These classes define a set of related names that have no underlying values associated with them. Use this kind of enumeration when you want descriptive names, but your application does not require specific information associated with the name.

See the WeekDays class in the Enumeration Class and the Define Methods in Enumeration Classes sections.

Enumerations with Built-In Class Behaviors

Enumeration classes that subclass MATLAB built-in classes inherit most of the behaviors of those classes. For example, an enumeration class derived from the double class inherits the mathematical, relational, and set operations that work with variables of the class.

Enumerations do not support the colon (:) operator, even if the superclass does.

Enumerations with Properties for Member Data

Enumeration classes that do not subclass MATLAB built-in numeric and logical classes can define properties. These classes can define constructors that set each member's unique property values.

The constructor can save input arguments in property values. For example, a Color class can specify a Red enumeration member color with three (Red, Green, Blue) values:

   Red (1,0,0)

Related Topics