MATLAB Object-Oriented Programming

(Joyce) #1

Define Method in Function File


To define a method in a separate file in the class folder, create the function in a file with
the .m extension. Do not use the method-end keywords in that file. Name the file with the
function name, as with any function.

In the myFunc.m file, implement the method:

function output = myFunc(obj,arg1,arg2)
...% code here
end

It is a good practice to declare the function signature in the classdef file in a methods
block:

classdef MyClass
methods
output = myFunc(obj,arg1,arg2)
end
...
end

Specify Method Attributes in classdef File


If you specify method attributes for a method that you define in a separate function file,
include the method signature in a methods block in the classdef file. This methods
block specifies the attributes that apply to the method.

For example, the following code shows a method with Access set to private in the
methods block. The method implementation resides in a separate file. Do not include the
function or end keywords in the methods block. Include only the function signature
showing input and output arguments.

classdef MyClass
methods (Access = private)
output = myFunc(obj,arg1,arg2)
end
end

In a file named myFunc.m, in the @MyClass folder, define the function:

9 Methods — Defining Class Operations

Free download pdf