MATLAB Object-Oriented Programming

(Joyce) #1

Methods and Functions


In this section...
“The Methods Block” on page 5-15
“Method Calling Syntax” on page 5-15
“Private Methods” on page 5-17
“More Detailed Information On Methods” on page 5-17
“Class-Related Functions” on page 5-17
“How to Overload Functions and Operators” on page 5-18
“Rules for Defining Methods in Separate Files” on page 5-18

The Methods Block


Define methods as MATLAB functions within a methods block, inside the classdef
block. The constructor method has the same name as the class and returns an initialized
object of the class. To create an object with property values that are unique to that
instance, assign values to properties in the class constructor. Terminate all method
functions with an end statement.
classdef ClassName
properties
PropertyName
end
methods
function obj = ClassName(arg1,...)
obj.PropertyName = arg1;
...
end
function ordinaryMethod(obj,arg1,...)
...
end
end
methods (Static)
function staticMethod(arg1,...)
...
end
end
end

Method Calling Syntax


MATLAB differs from languages like C++ and Java in that there is no special hidden class
object passed to all methods. You must pass an object of the class explicitly to the method.

Methods and Functions............................... 5-

Free download pdf