Enumeration classes can subclass MATLAB® built-in classes. Deriving an enumeration class from a built-in classes is useful to extend the usefulness of the enumeration members.
Enumeration inherit functionality from the built-in class.
You can associate a numeric or logical value with enumeration members.
For a more basic discussion of enumeration classes, see Define Enumeration Classes.
Enumeration classes derived from built-in numeric and logical classes cannot define properties.
If an enumeration class subclasses a built-in numeric class, the subclass inherits ordering and arithmetic operations that you can apply to the enumerated names.
For example, the
Results class subclasses
int32 built-in class. This class associates
an integer value with each of the four enumeration members —
classdef Results < int32 enumeration First (100) Second (50) Third (10) NoPlace (0) end end
The enumeration member inherits the methods of the
(except the colon operator). Use these enumerations like numeric values
(summed, sorted, averaged).
ans = 1
For example, use enumeration names instead of numbers to rank two teams:
Team1 = [Results.First, Results.NoPlace, Results.Third, Results.Second]; Team2 = [Results.Second, Results.Third, Results.First, Results.First];
int32 operations on these
ans = 160
ans = 40
ans = First First Second Third
Team1 > Team2
ans = 1 0 0 0
sum(Team1) < sum(Team2)
ans = 1
When you first refer to an enumeration class that derives from
a built-in class such as,
int32, MATLAB passes
the input arguments associated with the enumeration members to the
superclass constructor. For example, referencing the
means that MATLAB calls:
to initialize the
int32 aspect of this
Enumeration classes that derive from MATLAB built-in numeric and logical classes can define more than one name for an underlying value. The first name in the enumeration block with a given underlying value is the actual name for that underlying value and subsequent names are aliases.
Specify aliased names with the same superclass constructor argument as the actual name:
classdef Bool < logical enumeration No (0) Yes (1) off (0) on (1) end end
For example, the actual name of an instance of the
a = Bool.No
a = No
b = Bool.off
b = No
The actual underlying value associated with an enumeration member
is the value returned by the built-in superclass. For example, consider
Bool class defined with constructor arguments
that are of class
classdef Bool < logical enumeration No (0) Yes (100) end end
This class derives from the built-in
Therefore, underlying values for an enumeration member depend only
on what value
logical returns when passed that
a = Bool.Yes
a = Yes
ans = 1
FlowRate enumeration class defines three
classdef FlowRate < int32 enumeration Low (10) Medium (50) High (100) end end
Reference an instance of an enumeration member:
setFlow = FlowRate.Medium;
This statement causes MATLAB to call the default constructor
with the argument value of
50. MATLAB passes this argument to the
first superclass constructor (
int32(50) in this
case). The result is an underlying value of
a 32-bit integer for the
FlowRate subclasses a built-in numeric
int32), this class cannot define properties.
including a converter method. Programs can use the converter to obtain
the underlying value:
setFlow = FlowRate.Medium; int32(setFlow)
ans = 50
If an enumeration is a subclass of a built-in numeric class, you can convert from built-in numeric data to the enumeration using the name of the enumeration class. For example:
a = Bool(1)
a = Yes
An enumerated class also accepts enumeration members of its own class as input arguments:
ans = Yes
The converter returns an object of the same size as in input:
ans = No Yes
Create an empty enumeration array using the
ans = 0x0 empty Boolean enumeration.