MATLAB Object-Oriented Programming

(Joyce) #1
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)

14 Enumerations

Free download pdf