Define Enumeration Classes - MATLAB & Simulink (original) (raw)

Enumeration Class

Create an enumeration class by adding an enumeration block to a class definition. For example, the WeekDays class enumerates a set of days of the week.

classdef WeekDays enumeration Monday, Tuesday, Wednesday, Thursday, Friday end end

To execute the MATLABĀ® code in the following sections, place the WeekDays class definition in a .m file on your path.

Construct an Enumeration Member

Refer to an enumeration member using the class name and the member name:

For example, assign the enumeration member WeekDays.Tuesday to the variable today:

today = WeekDays.Tuesday;

today is a variable of class WeekDays:

Name Size Bytes Class Attributes

today 1x1 104 WeekDays

Convert to Superclass Value

If an enumeration class specifies a superclass, you can convert an enumeration object to the superclass by passing the object to the superclass constructor. However, the superclass constructor must be able to accept its own class as input and return an instance of the superclass. MATLAB built-in numeric classes, such as uint32, allow this conversion.

For example, the Bearing class derives from the uint32 built-in class:

classdef Bearing < uint32 enumeration North (0) East (90) South (180) West (270) end end

Assign the Bearing.East member to the variable a:

Pass a to the superclass constructor and return a uint32 value:

Name Size Bytes Class Attributes

a 1x1 4 Bearing
b 1x1 4 uint32

The uint32 constructor accepts an object of the subclass Bearing and returns an object of class uint32.

Define Methods in Enumeration Classes

Define methods in an enumeration class like any MATLAB class. For example, define a method calledisMeetingDay for the WeekDays enumeration class. The use case is that the user has a recurring meeting on Tuesdays. The method checks if the input argument is an instance of the WeekDays memberTuesday.

classdef WeekDays enumeration Monday, Tuesday, Wednesday, Thursday, Friday end methods function tf = isMeetingDay(obj) tf = WeekDays.Tuesday == obj; end end end

Call isMeetingDay with an instance of the WeekDays class:

today = WeekDays.Tuesday; today.isMeetingDay

You can also use the enumeration member as a direct input to the method:

isMeetingDay(WeekDays.Wednesday)

Define Properties in Enumeration Classes

Add properties to an enumeration class when you must store data related to the enumeration members. Set the property values in the class constructor. For example, theSyntaxColors class defines three properties. The class constructor assigns the values of the input arguments to the corresponding properties when you reference a class member.

classdef SyntaxColors properties R G B end methods function c = SyntaxColors(r, g, b) c.R = r; c.G = g; c.B = b; end end enumeration Error (1, 0, 0) Comment (0, 1, 0) Keyword (0, 0, 1) String (1, 0, 1) end end

When you refer to an enumeration member, the constructor initializes the property values:

e = SyntaxColors.Error; e.R

Because SyntaxColors is a value class (it does not derive from handle), only the class constructor can set property values:

You cannot set the read-only property 'R' of SyntaxColors.

For more information on enumeration classes that define properties, see Mutable Handle vs. Immutable Value Enumeration Members.

Enumeration Class Constructor Calling Sequence

Each statement in an enumeration block is the name of an enumeration member, optionally followed by an argument list. If the enumeration class defines a constructor, MATLAB calls the constructor to create the enumerated instances.

MATLAB provides a default constructor for all enumeration classes that do not explicitly define a constructor. The default constructor creates an instance of the enumeration class:

For example, the input arguments for the Bool class are 0 for Bool.No and 1 for Bool.Yes.

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

The values of 0 and 1 are of class logical because the default constructor passes the argument to the first superclass. That is, this statement:

Results in a call to logical that is equivalent to the following statement in a constructor:

function obj = Bool(val) obj@logical(val) end

MATLAB passes the member argument only to the first superclass. For example, suppose Bool derived from another class:

classdef Bool < logical & MyBool enumeration No (0) Yes (1) end end

The MyBool class can add some specialized behavior:

classdef MyBool methods function boolValues = testBools(obj) ... end end end

The default Bool constructor behaves as if defined like this function:

function obj = Bool(val) obj@logical(val) obj@MyBool
end