Enumerations for Property Values

Syntax for Property/Enumeration Definition

You can restrict the values that are allowed for a property to members of an enumeration class. Define the property as restricted to a specific enumeration class in the class definition using this syntax:

   PropName EnumerationClass

This syntax restricts values of PropName to members of the enumeration class EnumerationClass.

Example of Restricted Property

For example, the Days class defines a property named Today. The allowed values for the Today property are enumeration members of the WeekDays class.

The WeekDays class defines the enumerations:

classdef WeekDays
      Monday, Tuesday, Wednesday, Thursday, Friday

Use the WeekDays enumerations to restrict the allowed values of the Today property:

classdef Days
      Today WeekDays

Create an object of the Days class.

d = Days;
d.Today = WeekDays.Tuesday;
d = 

  Days with properties:

    Today: Tuesday

Representing Enumeration Members with char Vectors

The automatic conversion feature enables users of the Days class to assign values to the Today property as either enumeration members, char vectors, or string scalars. The Today property is restricted to members of the WeekDays enumeration class. Therefore, you can assign a char vector that represents a member of the WeekDays class.

d = Days;
d.Today = 'Tuesday';

Also, you can use a string scalar:

d = Days;
d.Today = "Tuesday";

For more information on restricting property values, see Validate Property Values and Property Class and Size Validation.