Refer to Enumerations
Instances of Enumeration Classes
Enumeration members are instances of the enumeration class. You can assign enumeration members to variables and form arrays of enumeration members. If an enumeration class derives from a superclass, you can substitute an enumeration member for an instance of the superclass.
The WeekDays
class defines enumeration members for five days of the week.
classdef WeekDays enumeration Monday, Tuesday, Wednesday, Thursday, Friday end end
Create objects of the WeekDays
class representing specific days.
today = WeekDays.Monday; tomorrow = WeekDays.Tuesday;
The variables today
and tomorrow
are objects of the WeekDays
class.
The PPM
class defines three enumeration members. Each member has an associated numeric value derived from the class superclass.
classdef PPM < double enumeration High (1000) Medium (100) Low (10) end end
Assign an enumeration member to a variable.
level = PPM.High;
When you substitute enumeration members for instances of the superclass, MATLAB® coerces the enumeration member to the superclass. For example, add a
numeric value to an enumeration member of the PPM
class.
levelNew = level + 100
levelNew = 1100
The result is of class double
.
whos
Name Size Bytes Class Attributes level 1x1 108 PPM levelNew 1x1 8 double
You can substitute superclass values for corresponding enumeration members. For
example, pass one of the numeric values defined in the enumeration class to the
PPMSwitch
function.
function PPMSwitch(ppm) switch ppm case PPM.Low disp Low case PPM.Medium disp Medium case PPM.High disp High end end
PPMSwitch(100)
Medium
You can also use an enumeration member directly:
PPMSwitch(PPM.Medium)
Medium
For information on operations you can perform on enumeration class instances, see Operations on Enumerations.
Conversion of Characters to Enumerations
Enumeration classes can convert char
vectors to enumeration members when the char
vector represents an enumeration member defined by the class. This conversion enables you to pass a valid char
vector or a cell array of char
vectors when enumerations are expected.
Use a char
vector instead of a direct reference to an enumeration
member when you want to use a simple character string to specify an enumeration member.
However, specifying an enumeration member directly eliminates the conversion from
char
to enumeration.
Enumeration classes provide a converter function using the constructor syntax.
today = WeekDays('Tuesday');
Because the char
vector 'Tuesday'
matches the enumeration member WeekDays.Tuesday
, the Weekdays
char
method can perform the conversion.
class(today)
ans = WeekDays
Create an enumeration array using the WeekDay
class constructor and a cell array of char
vectors.
wd = WeekDays({'Monday','Wednesday','Friday'})
wd = Monday Wednesday Friday
class(wd)
ans = WeekDays
All char
vectors in the cell array must correspond to an enumeration member defined by the class.
Coercion of char
to Enumerations
MATLAB coerces char
vectors into enumeration members when
the dominant argument is an enumeration. Because user-defined classes are dominant
over the char
class, MATLAB attempts to convert the char
vector to a member of
the enumeration class.
Create an enumeration array. Then insert a char
vector that represents an enumeration member into the array.
a = [WeekDays.Monday,WeekDays.Wednesday,WeekDays.Friday]
a = Monday Wednesday Friday
Add a char
vector to the WeekDays
array.
a(end+1) = 'Tuesday'
a = Monday Wednesday Friday Tuesday
MATLAB coerces the char
vector to a WeekDays
enumeration member.
class(a)
ans = WeekDays
Substitute Enumeration Members for char
Vectors
You can use enumeration members in place of char
vectors in cases where functions require char
vectors. For example, this call to sprintf
expects a char
vector, designated by the %s
format specifier.
sprintf('Today is %s',WeekDays.Friday)
ans = Today is Friday
The automatic conversion of enumeration classes to char
enable you to use enumeration members in this case.
Enumeration Arrays
Create enumeration arrays by:
Concatenating enumeration members using
[]
Assigning enumeration members to an array using indexed assignment
Create an enumeration array of class WeekDays
by concatenating enumeration members:
wd = [WeekDays.Tuesday,WeekDays.Wednesday,WeekDays.Friday];
Create an enumeration array of class WeekDays
by indexed assignment:
a(1) = WeekDays.Tuesday; a(2) = WeekDays.Wednesday; a(3) = WeekDays.Friday;
Mixed Enumeration Members and char
Vectors
You can concatenate enumeration members and char
vectors as long as the char
vector represents an enumeration member.
clear a a = [WeekDays.Wednesday,'Friday']; class(a)
ans = WeekDays
You can also assign a char
vector to an enumeration array:
clear a a(1) = WeekDays.Wednesday; a(2) = 'Friday'; class(a)
ans = WeekDays
Default Enumeration Member
The default member of an enumeration class is the first enumeration member defined
in the enumeration block. For the WeekDays
class, the default
enumeration member is WeekDays.Monday
.
classdef WeekDays enumeration Monday, Tuesday, Wednesday, Thursday, Friday end end
MATLAB allows assignment to any element of an array, even if the array variable does not previously exist. To fill in unassigned array elements, MATLAB uses the default enumeration member.
For example, assign a value to element 5 of an array, a
:
clear a
a(5) = WeekDays.Tuesday;
MATLAB must initialize the values of array elements a(1:4)
with the default enumeration member. The result of the assignment to the fifth element of the array a
is:
a
a = Monday Monday Monday Monday Tuesday