MATLAB Object-Oriented Programming

(Joyce) #1

Objects In Conditional Statements


In this section...
“Enable Use of Objects In Conditional Statements” on page 5-37
“How MATLAB Evaluates Switch Statements” on page 5-37
“How to Define the eq Method” on page 5-39
“Enumerations in Switch Statements” on page 5-41

Enable Use of Objects In Conditional Statements


Enable the use of objects in conditional statements by defining relational operators for
the class of the object. Classes that derive from the handle class inherit relational
operators. Value classes can implement operators to support the use of conditional
statements involving objects. For information on defining operators for your class, see
“Operator Overloading” on page 17-47.

How MATLAB Evaluates Switch Statements


MATLAB enables you to use objects in switch statements when the object’s class defines
an eq method. The eq method implements the == operation on objects of that class.

For objects, switch_expression == case_expression defines how MATLAB
evaluates switch and cases statements.

NoteYou do not need to define eq methods for enumeration classes. See “Enumerations
in Switch Statements” on page 5-41.

Handle Objects in Switch Statements

All classes derived from the handle class inherit an eq method. The expression,

h1 == h2

is true if h1 and h2 are handles for the same object.

For example, the BasicHandle class derives from handle:

Objects In Conditional Statements..................... 5-

Free download pdf