Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB   

Specifying Methods and Functions

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 object. You can assign values to properties in the class constructor. Terminate all method functions with an end statement.

classdef ClassName
   methods
      function obj = ClassName(arg1,arg2,...)
         obj.Prop1 = arg1;
         ...
      end
      function normal_method(obj,arg2,...)
         ...
      end
   end
   methods (Static = true)
      function static_method(arg1,...)
         ...
      end
   end
end

Methods In Separate Files

For classes that include many methods, you can use separate files for some or all the class methods. To use multiple files for a class definition, put the class file in an @-folder.

Define the Method Like Any Function

To define a method in a separate file in the class @-folder, create the function in a separate file, but do not use a method block in that file. Name the file with the function name, as with any function.

Methods That Must Be In the classdef File

You must put the following methods in the classdef file, not in separate files:

Specify Method Attributes in classdef File

If you specify method attributes for a method that you define in a separate file, Include the method signature in a methods block in the classdef block. For example, the following code shows a method with Access set to private in the classdef block. The method implementation resides in a separate file. Do not include the function or end keywords in the methods block, just the function signature showing input and output arguments.

classdef ClassName
% In a methods block, set the method attributes
% and add the function signature
   methods (Access = private)
      output = myFunc(obj,arg1,arg2)
      end
   end
end

In an M-file named myFunc.m, in the @-folder, define the function:

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

Include the method signature in the file with the classdef block only if you want to specify attributes for that method. Otherwise, you can implement the method as a function in a separate file in the @-folder.

Defining Static Methods in Separate Files

To create a static method, set the function's Static attribute to true. List any static methods that you define in separate files in the @-class folder. List these methods in the static methods block in the classdef file. Include the input and output arguments with the function name. For example:

classdef ClassName
...
   methods (Static)
      output = staticFunc1(arg1,arg2)
      staticFunc2
   end

You would then define the functions in separate files using the same function signature. For example:

function output = staticFunc1(arg1,arg2)
   ...
end

For an Example

The example, Example — Using Events to Update Graphs uses multiple files for class definition.

Defining Private Methods

Use the Access method attribute to create a private method. You do not need to use a private folder.

See Method Attributes for a list of method attributes.

More Detailed Information On Methods

See Class Methods for more information about methods.

Defining Class-Related Functions

You can define functions that are not class methods in the file that contains the class definition (classdef). Define subfunctions outside of the classdef - end block, but in the same file as the class definition. Subfunctions defined in classdef files work like subfunctions. You can call these subfunctions from anywhere in the same file, but they are not visible outside of the file in which you define them.

Subfunctions in classdef files are useful for utility functions that you use only within that file. These functions can take or return arguments that are instances of the class but, it is not necessary, as in the case of ordinary methods. For example, the following code defines myUtilityFcn outside the classdef block:

classdef MyClass
   properties
      PropName
   end
   methods
      function obj = MyClass(arg1)
         obj.PropName = arg1;
      end 
   end % methods
end % classdef
function myUtilityFcn
   ...
end

You also can create package functions, which require you to use the package name when calling these functions. See Scoping Classes with Packages for more information on packages

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS