Documentation

### This is machine translation

Translated by
Mouseover text to see original. Click the button below to return to the English version of the page.

## Define Enumeration Classes

### 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:

`ClassName.MemberName`

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

`today = WeekDays.Tuesday;`

`today` is a variable of class `WeekDays`:

`whos`
``` Name Size Bytes Class Attributes today 1x1 104 WeekDays ```
`today`
```today = Tuesday```

### Convert to Superclass Value

If an enumeration class specifies a superclass, 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`:

`a = Bearing.East;`

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

```b = uint32(a); whos```
``` Name Size Bytes Class Attributes a 1x1 60 Bearing b 1x1 4 uint32 ```

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

### Define Methods in Enumeration Classes

Define methods in an enumeration class like any MATLAB class. For example, here is the `WeekDays` class with a method called `isMeetingDay` added:

```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 ```
```ans = 0```

Use the enumeration member directly as input to the method:

```isMeetingDay(WeekDays.Wednesday) ```
```ans = 1```

### 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, the `SyntaxColors` class defines three properties whose values the constructor assigns to the values of the input arguments 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```
```ans = 1```

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

```e.R = 0 ```
`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:

• Using no input arguments, if the enumeration member defines no input arguments

• Using the input arguments defined in the enumeration class for that member

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:

`n = Bool.No;`

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:

• Argument passed to first superclass constructor

• No arguments passed to subsequent constructors

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