Accelerating the pace of engineering and science

# Documentation Center

• Trial Software

## Enumerations Derived from Built-In Types

### Basic Knowledge

The material presented in this section builds on an understanding of the information provided in the following sections.

### Why Derive Enumerations from Built-In Types

 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, which you can apply to the enumerated names.

For example, the Results class subclasses the int32 built-in class and 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)
NoPoints (0)
end
end```

Because the enumeration member inherits the methods of the int32 class (not the colon operator), you can use these enumerations like numeric values (summed, sorted, averaged, and so on).

```isa(Results.Second,'int32')

ans =

1
```

For example, use enumeration names instead of numbers to rank two teams:

```Team1 = [Results.First, Results.NoPoints, 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```

#### Creating 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.

### Aliasing 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 Boolean < logical
enumeration
No  (0)
Yes (1)
off (0)
on  (1)
end
end```

For example, the actual name of an instance of the Boolean.off enumeration member is No:

```a = Boolean.No

a =

No

b = Boolean.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 Boolean class defined with constructor arguments that are of class double:

```classdef Boolean < 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 = Boolean.Yes
a =

Yes

logical(a)

ans =

1```

#### Subclassing a Numeric Built-In Class

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

```classdef FlowRate < int32
enumeration
Low    (10)
Medium (50)
High   (100)
end
end```

Referencing an instance of an enumeration member:

`setFlow = FlowRate.Medium;`

returns an instance that is the result of MATLAB calling the default constructor with the argument value of 50. MATLAB passes this argument to the first superclass constructor (int32(50) in this case), which results in an underlying value of 50 as a 32-bit integer for the FlowRate.Medium member.

Because FlowRate subclasses a MATLAB built-in numeric class (int32), it cannot define properties. However FlowRate inherits int32 methods including a converter method, which programs can use 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, it is possible to convert from built-in numeric data to the enumeration using the name of the enumeration class. For example:

```a = Boolean(1)

a =

Yes```

An enumerated class also accepts enumeration members of its own class as input arguments:

```Boolean(a)

ans =

Yes```

Nonscalar inputs to the converter method return an object of the same size:

```Boolean([0,1])

ans =

No     Yes```

Create an empty enumeration array using the empty static method:

```Boolean.empty

ans =

0x0 empty Boolean enumeration.```