The basic purpose of a class is to define an object that encapsulates
data and the operations performed on that data. For example,
a property and two methods that operate on the data in that property:
Value — Property that contains
the data stored in an object of the class
roundOff — Method that rounds
the value of the property to two decimal places
multiplyBy — Method that
multiplies the value of the property by the specified number
Here is the definition of
classdef BasicClass properties Value 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
To use the class:
Save the class definition in a
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 an object of the class using the class name:
a = BasicClass
a = BasicClass with properties: Value: 
Initially, the property value is empty.
Assign a value to the
Value property using
the object variable and a dot before the property name:
a.Value = pi/3;
To access a property value, use dot notation without the assignment:
ans = 1.0472
For information on class properties, see Properties.
roundOff method on object
ans = 1.0500
Pass the object as the first argument to a method that takes multiple arguments:
ans = 3.1416
You can also call a method using dot notation:
It is not necessary to pass the object explicitly as an argument when using dot notation. The notation uses the object to the left of the method name.
For information on class methods, see Methods and Functions
Classes can define a special method to create objects, called
a constructor. Constructor methods enable you to pass arguments to
the constructor, and to validate and assign property values. Here
is a constructor for the
methods function obj = BasicClass(val) if nargin > 0 if isnumeric(val) obj.Value = val; else error('Value must be numeric') end end end end
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
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
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.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)); a(3) = BasicClass(7); roundOff(a)
ans = 2.8000 0.8700 7.0000
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
It makes sense to add the values of the
of each object.
Here is an overload of the MATLAB
It defines addition for this class as adding the property values:
method function r = plus(o1,o2) r = [o1.Value] + [o2.Value]; end end
By implementing a method called
can use the "
+" operator with objects
a = BasicClass(pi/3); b = BasicClass(pi/4); a + b
ans = 1.8326
For information on overloading functions, see Overload Functions in Class Definitions.
For information on overloading operators, see Operator Overloading.
Here is the
BasicClass definition after adding
the features discussed in this topic:
classdef BasicClass properties Value end methods function obj = BasicClass(val) if nargin == 1 if isnumeric(val) obj.Value = val; else error('Value must be numeric') end 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