MATLAB Object-Oriented Programming

(Joyce) #1

Named Values


In this section...
“Kinds of Predefined Names” on page 14-2
“Techniques for Defining Enumerations” on page 14-3

Kinds of Predefined Names


MATLAB supports two kinds of predefined names:


  • Constant properties

  • Enumerations


Constant Properties

Use constant properties when you want a collection of related constant values whose
values can belong to different types (numeric values, character strings, and so on). Define
properties with constant values by setting the property Constant attribute. Reference
constant properties by name whenever you need access to that particular value.

See “Define Class Properties with Constant Values” on page 15-2 for more information.

Enumerations

Use enumerations when you want to create a fixed set of names representing a single
type of value. Use this new type in multiple places without redefining it for each class.

You can derive enumeration classes from other classes to inherit the operations of the
superclass. For example, if you define an enumeration class that subclasses a MATLAB
numeric class like double or int32, the enumeration class inherits all the mathematical
and relational operations that MATLAB defines for those classes.

Using enumerations instead of character strings to represent a value, such as colors
('red'), can result in more readable code because:


  • You can compare enumeration members with == instead of using strcmp

  • Enumerations maintain type information, char vectors do not. For example, passing a
    char vector 'red' to functions means that every function must interpret what 'red'
    means. If you define red as an enumeration, the actual value of 'red' can change


14 Enumerations

Free download pdf