MATLAB Object-Oriented Programming

(Joyce) #1
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 enumerations 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

14 Enumerations

Free download pdf