MATLAB Object-Oriented Programming

(Joyce) #1

For information on operations you can perform on enumeration class instances, see
“Operations on Enumerations” on page 14-19.


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 strings 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


Refer to Enumerations
Free download pdf