Kinds of Predefined Names
MATLAB® supports two kinds of predefined names:
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
attribute. Reference constant properties by name whenever you need access to that
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
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
Enumerations maintain type information, while
charvectors do not. For example, passing a
'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
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,
if, and several comparison functions like
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.
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: