The material presented in this section builds on an understanding of the information provided in the following sections.
See for general information about these two kinds of classes.
Use a handle enumeration when you want to enumerate a set of objects whose state might change over time. Use a value enumeration to enumerate a set of abstract (and immutable) values.
A value-based enumeration class has a fixed set of specific values. You cannot modify these values by changing the values of properties because doing so expands or changes the fixed set of values for this enumeration class.
Value-based enumeration class implicitly define the
of all properties as
immutable. You cannot set
SetAccess attribute to any other value.
However, all superclass properties must explicitly define property
See Property Attributes for more
information on property attributes.
When you create an instance of a value-based enumeration class, this instance is unique until the class is cleared and reloaded. For example, given the following class:
classdef WeekDays enumeration Monday, Tuesday, Wednesday, Thursday, Friday end end
a = WeekDays.Monday; b = WeekDays.Monday; isequal(a,b) ans = 1 a == b ans = 1
Value-based enumeration classes that define properties are immutable.
For example, the
Colors enumeration class associates
RGB values with color names.
classdef Colors properties R = 0; G = 0; B = 0; end methods function c = Colors(r, g, b) c.R = r; c.G = g; c.B = b; end end enumeration Red (1, 0, 0) Green (0, 1, 0) Blue (0, 0, 1) end end
The constructor assigns the input arguments to
red = Colors.Red;
You cannot change a property value:
red.G = 1; Setting the 'G' property of the 'Colors' class is not allowed.
Handle-based enumeration classes that define properties are
mutable. Derive enumeration classes from the
handle class when you must be able to change
property values on instances of that class.
You cannot derive an enumeration class from
Given a handle-based enumeration class with properties, changing the property value of an instance causes all references to that instance to reflect the changed value.
For example, the
class associates RGB values with color names, the same as the
in the previous example. However,
classdef HandleColors < handle % Enumeration class derived from handle properties R = 0; G = 0; B = 0; end methods function c = HandleColors(r, g, b) c.R = r; c.G = g; c.B = b; end end enumeration Red (1, 0, 0) ... % Other colors omitted end end
Create an instance of
return the value of the
a = HandleColors.Red; a.R ans = 1
MATLAB constructs the
member, which sets the
R property to
G property to
0, and the
Change the value of the
R property to
a.R = .8;
After setting the value of the
.8, create another instance,
b = HandleColors.Red; b.R ans = 0.8000
The value of the
R property of the newly
created instance is also
0.8000. The MATLAB session
has only one value for any enumeration member at any given time.
Clearing the workspace variables does not change the current
definition of the enumeration member
clear a = HandleColors.Red; a.R ans = 0.8000
Clear the class to reload the definition of the
clear classes a = HandleColors.Red; a.R ans = 1
If you do not want to allow reassignment of a given property
value, set that property's
See Property Attributes for more information about property attributes.
Suppose you assign two variables to a particular enumeration member:
a = HandleColors.Red; b = HandleColors.Red;
You can compare
>> isequal(a,b) ans = 1
The property values of
the same, so
However, unlike nonenumeration handle classes,
the same handle because there is only one enumeration member. Determine
handle equality using
== (the handle
>> a == b ans = 1
for information on how
when used with handles.
MachineState class defines two enumeration
members to represent the state of a machine, either running or not
classdef MachineState enumeration Running NotRunning end end
Machine class represents a machine with
start and stop operations. The
are easy to work with because of their
and they result in code that is easy to read.
classdef Machine < handle properties (SetAccess = Private) State = MachineState.NotRunning; end methods function start(machine) if machine.State == MachineState.NotRunning machine.State = MachineState.Running; end disp (machine.State.char) end function stop(machine) if machine.State == MachineState.Running machine.State = MachineState.NotRunning; end disp (machine.State.char) end end end
Machine object and call
% Create a Machine object >> m = Machine; % Start the machine >> m.start Running % Stop the machine >> m.stop NotRunning