Main Content

Operations on Enumerations

Operations Supported by Enumerations

You can use logical, set membership, and string comparison operations on enumerations. These operations also allow the use of enumeration in conditional statements, such as switch and if statements. Converters enable you to use char vectors and cell arrays of strings as enumerations.

Enumeration Class

The WeekDays class defines members that enumerate days 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

For information on defining enumerations, see Define Enumeration Classes.

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}])
ans =

char

Enumerations and char Vectors in Relational Operations

Expression involving the relational operators, eq and ne, in which one operand is an enumeration allow the other operand to be of type char. Before performing the operation, MATLAB® converts char vectors to scalar enumerations or cell arrays of char vectors to enumeration arrays.

Note

Enumeration classes that derive from MATLAB built-in classes cannot substitute char vectors for enumeration members.

today = WeekDays.Friday;
today == 'Friday'
ans =

     1

Compare enumeration array to char vector:

wd = [WeekDays.Monday,WeekDays.Wednesday,WeekDays.Friday];
wd == 'Friday'
ans =

     0     0     1

Compare enumeration array to cell array of char vectors:

cv = {'Monday','Wednesday','Friday'};
md = [WeekDays.Tuesday,WeekDays.Thursday,WeekDays.Friday];
md ~= cv
ans =

     1     1     0

The char vector Wednesday is equal to (==) the 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

Enumerations in switch Statements

Equality (eq) and inequality (ne) methods enable you to use enumeration members in switch statements. For example, using the WeekDays class defined previously, 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 a member of the WeekDays enumeration class to the Reminder function:

today = WeekDays.Wednesday;
Reminder(today)
ans =

Team meeting at 2:00

For more information, see Objects In Conditional Statements.

Substitute char Vectors

Note

Enumeration classes that derive from MATLAB built-in classes cannot substitute char vectors for enumeration members.

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 specifying enumerations explicitly, MATLAB must convert the char to an enumeration. Eliminate the need for this conversion if it is not necessary.

Enumeration Set Membership

Enumeration classes provide methods to determine set membership.

  • ismember — True for elements of an enumeration array if in a set

  • setdiff — Set difference for enumeration arrays

  • intersect — Set intersection for enumeration arrays

  • setxor — Set exclusive-or for enumeration arrays

  • union — Set union for enumeration arrays

Determine if today is a meeting day for your team. Create a set of enumeration members corresponding to the days on which the team has meetings.

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

Use ismember to determine if today is part of the teamMeetings set:

ismember(today,teamMeetings)
ans = 
     0

Mixed Sets of Enumeration and char

If you pass both enumeration and char arguments to an enumeration class method, the class attempts to convert the char to the class of the enumeration.

Determine if char vector is a member of the enumeration array.

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

     1

Determine if the enumeration member is a member of the cell array of char vectors.

ismember(WeekDays.Friday,{'Wednesday','Friday'})
ans =

     1

Enumeration Text Comparison Methods

Enumeration classes provide methods to compare enumeration members with char vectors. One of the arguments to the string comparison method must be a char vector. Comparing two enumeration members returns false.

  • strcmp — Compare enumeration members

  • strncmp — Compare first n characters of enumeration members

  • strcmpi — Case insensitive comparison of enumeration members

  • strncmpi — Case insensitive first n character comparison of enumeration members

Comparing Enumeration Member with char Vector

The string comparison methods 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 Enumerations

Obtain information about enumeration classes using the enumeration function. For example:

enumeration WeekDays
Enumeration members for class 'WeekDays':

    Monday
    Tuesday
    Wednesday
    Thursday
    Friday

See also Metaclass EnumeratedValues Property

Testing for an Enumeration

To determine if a variable is an enumeration, use the isenum function. For example:

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

     1

isenum returns true for empty enumeration objects:

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

     1

To determine if the class of a variable class is an enumeration class, use the meta.class object.

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

     1

Related Topics