catalogue
Enumeration operation
Enumerating supported operations
You can use logic, set relationships, and string comparison operations on enumerations. These operations also allow enumerations to be used in conditional statements, such as switch and if statements. The converter can use char vectors and string cell arrays as enumerations.
Enumeration class
The WeekDays class defines members that enumerate workdays of the week. This topic uses the WeekDays class to illustrate how to perform operations on enumerations.
classdef WeekDays enumeration Monday, Tuesday, Wednesday, Thursday, Friday end end
Default method
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 a char vector, a cell array or a string array element of a char vector into an enumeration. When used in conjunction with enumeration, other methods behave similarly to equivalent functions.
Convert enumeration members to characters
Converting to char is useful because enumeration members can be defined with descriptive names. For example:
today = WeekDays.Friday; ['Today is ',char(today)] ans = Today is Friday
Converts an enumerated array to a character vector cell array
Use cellstr to convert an enumerated array to a char vector cell array.
ca = cellstr([WeekDays.Tuesday,WeekDays.Thursday]); class(ca) ans = cell
Both cells in the cell array contain char vectors:
class([ca{1:2}]) ans = char
Enumerations and character vectors in relational operations
For expressions involving the relational operators eq and ne, if one operand is an enumeration, the other operand is allowed to be of type char. Before performing the operation, MATLAB ® Convert a char vector to an enumerated scalar or a char vector cell array to an enumerated array.
Note: enumeration classes derived from MATLAB built-in classes cannot replace enumeration members with char vectors.
today = WeekDays.Friday; today == 'Friday' ans = 1
Compare the enumerated array with the char vector:
wd = [WeekDays.Monday,WeekDays.Wednesday,WeekDays.Friday]; wd == 'Friday' ans = 0 0 1
Compare the enumeration array with the char vector cell array:
cv = {'Monday','Wednesday','Friday'}; md = [WeekDays.Tuesday,WeekDays.Thursday,WeekDays.Friday]; md ~= cv ans = 1 1 0
char vector Wednesday is equal to (= =) enumeration member weekdays Wednesday. You can use this equality in conditional statements:
today = 'Wednesday'; ... if today == WeekDays.Wednesday disp('Team meeting at 2:00') end
Enumeration in switch} statement
Equal (eq) and unequal (ne) methods can use enumerated members in switch statements. For example, use the WeekDays class defined earlier to construct a switch statement:
function c = Reminder(day) % Add error checking here switch(day) case WeekDays.Monday c = 'Department meeting at 10:00'; case WeekDays.Tuesday c = 'Meeting Free Day!'; case {WeekDays.Wednesday WeekDays.Friday} c = 'Team meeting at 2:00'; case WeekDays.Thursday c = 'Volleyball night'; end end
Pass the members of the WeekDays enumeration class to the Reminder function:
today = WeekDays.Wednesday; Reminder(today) ans = Team meeting at 2:00
Instead of , char , vector
Note: enumeration classes derived from MATLAB built-in classes cannot replace enumeration members with char vectors. You can use char vectors to represent specific enumeration members:
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
Although you can use char vectors instead of explicitly specifying enumerations, MATLAB must convert char to enumerations. If not necessary, this conversion is not required.
Enumerating collection relationships
Enumeration classes provide methods for determining collection relationships.
-
isemenber - true if the elements of the enumerated array are in the collection
-
setdiff - enumerates the difference set of the array
-
intersect - enumerates the intersection of arrays
-
setxor - enumerates the exclusive or set of arrays
-
Union - union of enumerated arrays
Determine whether today is the date for the team meeting. Create a set of enumerated members corresponding to the team meeting date.
today = WeekDays.Tuesday; teamMeetings = [WeekDays.Wednesday WeekDays.Friday];
Use ismember to determine whether today belongs to the teamMeetings collection:
ismember(today,teamMeetings) ans = 0
A mixed set of enumeration and # char #
If you pass both enumeration and char parameters to an enumeration class method, the class attempts to convert char to an enumerated class. Determines whether the char vector is a member of an enumerated array.
teamMeetings = [WeekDays.Wednesday WeekDays.Friday]; ismember('Friday',teamMeetings) ans = 1
Determines whether the enumeration member is a member of the char vector cell array.
ismember(WeekDays.Friday,{'Wednesday','Friday'}) ans = 1
Enumeration text comparison method
The enumeration class provides methods to compare enumeration members with char vectors. One of the parameters of the string comparison method must be a char vector. Comparing two enumeration members returns false.
-
strcmp - compare enumeration members
-
strncmp - compare the first n characters of enumeration members
-
strcmpi - case insensitive comparison of enumeration members
-
strncmpi - case insensitive comparison of the first n characters of enumeration members
Comparison between enumeration members and {char} vectors
The string comparison method can compare enumeration members and char vectors.
today = WeekDays.Tuesday; strcmp(today,'Friday') ans = 0 strcmp(today,'Tuesday') ans = 1
How to get information about enumeration
Use the enumeration function to get information about the enumeration class. For example:
enumeration WeekDays Enumeration members for class 'WeekDays': Monday Tuesday Wednesday Thursday Friday
Testing enumeration
To determine whether a variable is an enumeration, use the isenum function. For example:
today = WeekDays.Wednesday; isenum(today) ans = 1
isenum returns true for empty enumerated objects:
noday = WeekDays.empty; isenum(noday) ans = 1
To determine whether the class of a variable class is an enumeration class, use meta Class object.
today = WeekDays.Wednesday; mc = metaclass(today); mc.Enumeration ans = 1