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.
TheWeekDays
class defines enumeration members for five days of the week.
classdefWeekDaysenumerationMonday, Tuesday, Wednesday, Thursday, Fridayendend
Create objects of theWeekDays
class representing specific days.
today = WeekDays.Monday; tomorrow = WeekDays.Tuesday;
The variablestoday
andtomorrow
are objects of theWeekDays
class.
ThePPM
class defines three enumeration members. Each member has an associated numeric value derived from the class superclass.
classdefPPM < doubleenumerationHigh (1000) Medium (100) Low (10)endend
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 an enumeration member of thePPM
class with a numeric values
levelNew = level + 100
levelNew = 1100
The result is of classdouble
.
whos
Name Size Bytes Class Attributes level 1x1 108 PPM levelNew 1x1 8 double
You can substitute superclass values for enumeration members when the values correspond. For example, pass one of the numeric values defined in the enumeration class to thePPMSwitch
function.
functionPPMSwitch(ppm)switchppmcasePPM.Low dispLowcasePPM.Medium dispMediumcasePPM.High dispHighendend
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, seeOperations on Enumerations.
Conversion of Characters to Enumerations
Enumeration classes can convertchar
vectors to enumeration members when thechar
vector represents an enumeration member defined by the class. This conversion enables you to pass a validchar
vector or a cell array ofchar
vectors when enumerations are expected.
Use achar
vector instead of a direct reference to an enumeration member when you want to use a simple character strings to specify an enumeration member. However, specifying an enumeration member directly eliminates the conversion fromchar
to enumeration.
Enumeration classes provide a converter function using the constructor syntax.
today = WeekDays('Tuesday');
Because thechar
vector'Tuesday'
matches the enumeration memberWeekDays.Tuesday
,Weekdays
char
method can perform the conversion.
class(today)
ans =工作日
Create an enumeration array using theWeekDay
class constructor and a cell array ofchar
vectors.
wd = WeekDays({'Monday','Wednesday','Friday'})
wd = Monday Wednesday Friday
class(wd)
ans =工作日
Allchar
vectors in the cell array must correspond to an enumeration member defined by the class.
强迫的char
to Enumerations
MATLAB coerceschar
vectors into enumerations members when the dominant argument is an enumeration. Because user-defined classes are dominant over thechar
class, MATLAB attempts to convert thechar
vector to a member of the enumeration class.
Create an enumeration array. Then insert achar
vector that represents an enumeration member into the array.
a = [WeekDays.Monday,WeekDays.Wednesday,WeekDays.Friday]
a = Monday Wednesday Friday
Add achar
vector to theWeekDays
array.
a(end+1) ='Tuesday'
a = Monday Wednesday Friday Tuesday
MATLAB coerces thechar
vector to aWeekDays
enumeration member.
class(a)
ans =工作日
Substitute Enumeration Members forchar
Vectors
You can use enumeration members in place ofchar
vectors in cases where functions requirechar
vectors. For example, this call tosprintf
expects achar
vector, designated by the%s
format specifier.
sprintf('Today is %s',WeekDays.Friday)
ans = Today is Friday
The automatic conversion of enumeration classes tochar
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 classWeekDays
by concatenating enumeration members:
wd = [WeekDays.Tuesday,WeekDays.Wednesday,WeekDays.Friday];
Create an enumeration array of classWeekDays
by indexed assignment:
a(1) = WeekDays.Tuesday; a(2) = WeekDays.Wednesday; a(3) = WeekDays.Friday;
Mixed Enumeration Members andchar
Vectors
You can concatenate enumeration members andchar
vectors as long as thechar
vector represents an enumeration member.
clearaa = [WeekDays.Wednesday,'Friday']; class(a)
ans =工作日
You can also assign achar
vector to an enumeration array:
clearaa(1) = WeekDays.Wednesday; a(2) ='Friday'; class(a)
ans =工作日
Default Enumeration Member
The default member an enumeration class is the first enumeration member defined in the enumeration block. For theWeekDays
class, the default enumeration member isWeekDays.Monday
.
classdefWeekDaysenumerationMonday, Tuesday, Wednesday, Thursday, Fridayendend
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
:
clearaa(5) = WeekDays.Tuesday;
MATLAB must initialize the values of array elementsa(1:4)
with the default enumeration member. The result of the assignment to the fifth element of the arraya
is:
a
a = Monday Monday Monday Monday Tuesday