Enumerations Derived from Built-In Classes

Subclassing Built-In Classes

Enumeration classes can subclass MATLAB® built-in classes. Deriving an enumeration class from built-in classes is useful to extend the usefulness of the enumeration members.

• Enumerations 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.

Derive Enumeration Class from Numeric Class

Note

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 the int32 built-in class. This class associates an integer value with each of the four enumeration members — First, Second, Third, and NoPoints.

classdef Results < int32
enumeration
First   (100)
Second  (50)
Third   (10)
NoPlace (0)
end
end

The enumeration member inherits the methods of the int32 class (except the colon operator). Use these enumerations like numeric values (summed, sorted, averaged).

isa(Results.Second,'int32')
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];

Perform int32 operations on these Results enumerations:

sum(Team1)
ans =

160
mean(Team1)
ans =

40
sort(Team2,'descend')
ans =

First      First      Second     Third
Team1 > Team2
ans =

1     0     0     0
sum(Team1) < sum(Team2)
ans =

1

How to Create Enumeration Instances

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 Second Results member, defined as:

Second (50)

means that MATLAB calls:

int32(50)

to initialize the int32 aspect of this Results object.

How to Alias Enumeration Names

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 Bool.off enumeration member is No:

a = Bool.No
a =

No
b = Bool.off
b =

No

Superclass Constructor Returns Underlying Value

The actual underlying value associated with an enumeration member is the value returned by the built-in superclass. For example, consider the Bool class defined with constructor arguments that are of class double:

classdef Bool < logical
enumeration
No  (0)
Yes (100)
end
end

This class derives from the built-in logical class. Therefore, underlying values for an enumeration member depend only on what value logical returns when passed that value:

a = Bool.Yes
a =

Yes
logical(a)
ans =

1

How to Subclass Numeric Built-In Classes

The FlowRate enumeration class defines three members, Low, Medium, and High.

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 50 as a 32-bit integer for the FlowRate.Medium member.

Because FlowRate subclasses a built-in numeric class (int32), this class cannot define properties. However FlowRate inherits int32 methods including a converter method. Programs can use the converter to obtain the underlying value:

setFlow = FlowRate.Medium;
int32(setFlow)
ans =

50

Default Converter

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:

Bool(a)
ans =

Yes

The converter returns an object of the same size as in input:

Bool([0,1])
ans =

No     Yes

Create an empty enumeration array using the empty static method:

Bool.empty
ans =

0x0 empty Boolean enumeration.