## 枚举的运算

### 枚举类示例

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

### 默认方法

`methods('WeekDays')`
```Methods for class WeekDays: WeekDays char intersect ne setxor strcmpi strncmp union cellstr eq ismember setdiff strcmp string strncmpi ```

`WeekDays` 方法将文本格式转换为枚举。支持的格式包括字符串、`char` 向量、字符串数组和 `char` 向量元胞数组。例如：

`f = WeekDays(["Monday" "Friday"])`
```f = 1×2 WeekDays enumeration array Monday Friday```

### 将枚举成员转换为字符串或 `char` 向量

`string(WeekDays.Monday) + " is our meeting day."`
```ans = "Monday is our meeting day."```

`['Today is ' char(WeekDays.Friday) '.']`
```ans = 'Today is Friday.'```

### 将枚举数组转换为字符串数组或 `char` 向量元胞数组

```sa = [WeekDays.Tuesday WeekDays.Thursday]; string(sa)```
```ans = 1×2 string array "Tuesday" "Thursday"```

```ca = cellstr([WeekDays.Tuesday WeekDays.Thursday]); class(ca)```
```ans = 'cell' ```

`class([ca{1:2}])`
```ans = 'char'```

### 枚举、字符串和 `char` 向量的关系运算

#### 关系运算符 `eq` 和 `ne`

```today = WeekDays.Friday; today == "Friday"```
```ans = logical 1```

```wd = [WeekDays.Monday WeekDays.Wednesday WeekDays.Friday]; wd == 'Friday'```
```ans = 1×3 logical array 0 0 1```

```sa = ["Monday" "Wednesday" "Friday"]; md = [WeekDays.Tuesday WeekDays.Thursday WeekDays.Friday]; md ~= sa```
```ans = 1×3 logical array 1 1 0```

`char` 向量 `Wednesday` 等于 (`==`) 枚举成员 `WeekDays.Wednesday`。您可以在条件语句中使用此相等性：

```today = 'Wednesday'; ... if today == WeekDays.Wednesday disp('Team meeting at 2:00') end```

#### `isequal` 方法

`isequal` 方法还支持枚举实例与字符串、字符向量、字符串数组和字符向量元胞数组之间的比较。

```a = WeekDays.Monday; isequal(a,"Monday")```
```ans = logical 1```

```wd = [WeekDays.Monday WeekDays.Wednesday WeekDays.Friday]; isequal(wd,"Friday")```
```ans = logical 0```

### `switch` 语句中的枚举

```function c = Reminder(day) % Add error checking here switch(day) case WeekDays.Monday c = 'No meetings'; case WeekDays.Tuesday c = 'Department meeting at 10:00'; case {WeekDays.Wednesday WeekDays.Friday} c = 'Team meeting at 2:00'; case WeekDays.Thursday c = 'Volleyball night'; end end```

`WeekDays` 枚举类的成员传递给 `Reminder` 函数：

```today = WeekDays.Wednesday; Reminder(today) ```
```ans = Team meeting at 2:00```

#### 代替字符串或 `char` 向量

```function c = Reminder2(day) switch(day) case 'Monday' c = 'Department meeting at 10:00'; case 'Tuesday' c = 'Meeting Free Day!'; case {'Wednesday' 'Friday'} c = 'Team meeting at 2:00'; case 'Thursday' c = 'Volleyball night'; end end```

### 枚举集合关系

```today = WeekDays.Tuesday; teamMeetings = [WeekDays.Wednesday WeekDays.Friday];```

```ismember(today,teamMeetings) ```
```ans = 0```

#### 枚举和文本的混合集

```teamMeetings = [WeekDays.Wednesday WeekDays.Friday]; ismember('Friday',teamMeetings)```
```ans = logical 1```

`ismember(WeekDays.Friday,["Wednesday" "Friday"])`
```ans = logical 1```

### 枚举的文本比较方法

#### 将枚举成员与字符串或 `char` 向量进行比较

```today = WeekDays.Tuesday; strcmp(today,'Friday')```
```ans = 0```
`strcmp(today,"Tuesday")`
```ans = 1```

### 获取有关枚举的信息

```enumeration WeekDays ```
```Enumeration members for class 'WeekDays': Monday Tuesday Wednesday Thursday Friday```

### 对枚举的测试

```today = WeekDays.Wednesday; isenum(today)```
```ans = 1```

```noday = WeekDays.empty; isenum(noday)```
```ans = 1```

```today = WeekDays.Wednesday; mc = metaclass(today); mc.Enumeration```
```ans = 1```