Creating a Simple Class
Design Class
The basic purpose of a class is to define an object that encapsulates data and the operations performed on that data. For example, BasicClass defines a property and two methods that operate on the data in that property:
Value— Property that contains the numeric data stored in an object of the classroundOff— Method that rounds the value of the property to two decimal placesmultiplyBy— Method that multiplies the value of the property by the specified number
Start a class definition with a classdef
block, and then define the
class properties and methods inside that block. Here is the definition of
ClassName...endBasicClass:
classdef BasicClass properties Value {mustBeNumeric} end methods function r = roundOff(obj) r = round([obj.Value],2); end function r = multiplyBy(obj,n) r = [obj.Value]*n; end end end
For a summary of class syntax, see classdef.
To use the class:
Save the class definition in a
.mfile with the same name as the class.Create an object of the class.
Access the properties to assign data.
Call methods to perform operation on the data.
Create Object
Create an object of the class using the class name:
a = BasicClass
a =
BasicClass with properties:
Value: []Initially, the property value is empty.
Access Properties
Assign a value to the Value property using the object variable and a dot before the property name:
a.Value = pi/3;
To return a property value, use dot notation without the assignment:
a.Value
ans =
1.0472
For information on class properties, see Property Syntax.
Call Methods
Call the roundOff method on object a:
roundOff(a)
ans =
1.0500Pass the object as the first argument to a method that takes multiple arguments, as in this call to the multiplyBy method:
multiplyBy(a,3)
ans =
3.1416You can also call a method using dot notation:
a.multiplyBy(3)
Passing the object as an explicit argument is not necessary when using dot notation. The notation uses the object to the left of the dot.
For information on class methods, see Method Syntax.
Add Constructor
Classes can define a special method to create objects of the class, called a constructor. Constructor methods enable you to pass arguments to the constructor, which you can assign as property values. The BasicClass
Value property restricts its possible values using the mustBeNumeric function.
Here is a constructor for the BasicClass class. When you call the constructor with an input argument, it is assigned to the Value property. If you call the constructor without an input argument, the Value property has a default value of empty ([]).
methods function obj = BasicClass(val) if nargin == 1 obj.Value = val; end end end
By adding this constructor to the class definition, you can create an object and set the property value in one step:
a = BasicClass(pi/3)
a =
BasicClass with properties:
Value: 1.0472The constructor can perform other operations related to creating objects of the class.
For information on constructors, see Class Constructor Methods.
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 5MATLAB 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.
[obj.Value] + 2
This syntax enables the method to work with arrays of objects. For example, create an object array using indexed assignment.
obj(1) = BasicClass(2.7984); obj(2) = BasicClass(sin(pi/3)); obj(3) = BasicClass(7);
These two expressions are equivalent.
[obj.Value] + 2 [obj(1).Value obj(2).Value obj(3).Value] + 2
The roundOff method is vectorized because the property reference
is enclosed in
brackets.
r = round([obj.Value],2);
roundOff is vectorized, it can operate on arrays.roundOff(obj)
ans =
2.8000 0.8700 7.0000Overload Functions
Classes can implement existing functionality, such as addition, by defining a method with the same name as the existing MATLAB function. For example, suppose that you want to add two BasicClass objects. It makes sense to add the values of the Value properties of each object.
Here is an overloaded version of the MATLAB
plus function. It defines addition for the BasicClass class as adding the property values:
methods function r = plus(o1,o2) r = [o1.Value] + [o2.Value]; end end
By implementing a method called plus, you can use the “+” operator with objects of BasicClass.
a = BasicClass(pi/3); b = BasicClass(pi/4); a + b
ans =
1.8326By vectorizing the plus method, you can operate on object arrays.
a = BasicClass(pi/3); b = BasicClass(pi/4); c = BasicClass(pi/2); ar = [a b]; ar + c
ans =
2.6180 2.3562Related Information
For information on overloading functions, see Overload Functions in Class Definitions.
For information on overloading operators, see Operator Overloading.
BasicClass Code Listing
Here is the BasicClass definition after adding the features discussed in this topic:
classdef BasicClass properties Value {mustBeNumeric} end methods function obj = BasicClass(val) if nargin == 1 obj.Value = val; end end function r = roundOff(obj) r = round([obj.Value],2); end function r = multiplyBy(obj,n) r = [obj.Value] * n; end function r = plus(o1,o2) r = [o1.Value] + [o2.Value]; end end end