MATLAB Object-Oriented Programming

(Joyce) #1

By adding this constructor to the class definition, you can create an object in one step:


a = BasicClass(pi/3)


a =


BasicClass with properties:


Value: 1.0472


This constructor also performs type checking on the input argument. For example:


a = BasicClass('A character array')


Error using BasicClass (line 11)
Value must be numeric


For information on constructors, see “Class Constructor Methods” on page 9-21


Vectorize Methods


MATLAB enables you to vectorize operations. For example, you can add a number to a
vector:


[1 2 3] + 2


ans =


3 4 5


MATLAB adds the number 2 to each of the elements in the array [1 2 3]. To vectorize
the arithmetic operator methods, enclose the obj.Value property reference in brackets,
where obj is an object array.


[obj.Value] + 2


This syntax enables the method to work with arrays of object. For example, given objects
a1, a2, and a3:


[a1.Value,a2.Value,a3.Value] + 2


By using vector notation, a can be an array:


a(1) = BasicClass(2.7984);
a(2) = BasicClass(sin(pi/3));


Create a Simple Class
Free download pdf