MATLAB® supports two kinds of predefined names:
Use constant properties when you want a collection of related
constant values whose values can belong to different types (numeric
values, character strings, and so on). Define properties with constant
values by setting the property
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
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 (
can result in more readable code because:
You can compare enumeration members with
Enumerations maintain type information,
do not. For example, passing a
functions means that every function must interpret what
If you define red as an enumeration, the actual value of
[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
in the class definition.
See Define Enumeration Classes for more information.
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.
if, and several comparison
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 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.
Enumeration classes that subclass MATLAB built-in
classes inherit most of the behaviors of those classes. For example,
an enumeration class derived from the
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.
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
member color with three (Red, Green, Blue) values:
enumeration Red (1,0,0) end