MATLAB Object-Oriented Programming

(Joyce) #1

Default Methods


Enumeration classes have the following default methods:

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

The WeekDays method converts char vectors, a cell array of char vectors, or string
array elements to enumerations.

Other methods behave similarly to the equivalent function when used with enumerations.
For information on a specific method, see the documentation for that function.

Convert Enumeration Member to Characters


Conversion to char is useful because you can define enumeration members with
descriptive names. For example:

today = WeekDays.Friday;
['Today is ',char(today)]

ans =

Today is Friday

Convert Enumeration Array to Cell Array of char Vectors


Use cellstr to convert an enumeration array to a cell array of char vectors.

ca = cellstr([WeekDays.Tuesday,WeekDays.Thursday]);
class(ca)

ans =

cell

Both cells in the cell array contain char vectors:

class([ca{1:2}])

14 Enumerations

Free download pdf