MATLAB Object-Oriented Programming

(Joyce) #1
function myUtilityFcn
...
end

You also can create package functions, which require the use of the package name when
calling these functions.

How to Overload Functions and Operators


Overload MATLAB functions for your class by defining a class method with the same
name as the function that you want to overload. MATLAB dispatches to the class method
when the function is called with an instance of the class.

You can overload MATLAB arithmetic, logical, relational, and indexing operators by
defining class methods with the appropriate names.

See the handle class for a list of operations defined for that class. All classes deriving
from handle inherit these methods.

Rules for Defining Methods in Separate Files


The following rules apply to methods defined in separate files:


  • To specify attributes for a method defined in a separate file, declare this method in a
    methods block in the classdef file. Specify the attribute values with the methods
    block.

  • The syntax declared in the methods block (if used) must match the method's
    function line.

  • The separate file must be in the class folder.

  • The class constructor method must be defined in the classdef file. The constructor
    cannot be in a separate file.

  • Handle class delete methods must be defined in the classdef file. The delete
    method cannot be in a separate file.


All functions that use dots in their names must be defined in the classdef file,
including:


  • Converter methods that must use the package name as part of the class name
    because the class is contained in packages


5 Class Definition—Syntax Reference

Free download pdf