08 Object-Oriented Programming (OOP) in MATLAB

Overview: Object-Oriented MATLAB Classes

MATLAB supports object-oriented programming. Classes organize properties and methods into encapsulated objects, saved in .m files matching the class name.


1. Class Structure: classdef

A MATLAB class is defined inside a classdef block:

classdef Waypoint
    properties
        latitude
        longitude
    end
    methods
        function obj = Waypoint(lat, lon)
            obj.latitude = lat;
            obj.longitude = lon;
        end
    end
end

Key Blocks:

  • classdef: Declares the class name. The class file name must match this name (e.g. Waypoint.m).
  • properties: Declares the member variables of the class (access behaves like dynamic structures).
  • methods: Declares functions that operate on the object.
    • Constructor: A function inside the methods block that has the exact same name as the class. It initializes and returns a class instance.

2. Invoking Object Methods

Unlike C++ or Python, all methods inside the class block must explicitly list the object instance as their first parameter:

function r = scaleLatitude(obj, scaleFactor)
    r = scaleFactor * obj.latitude;
end

Invocation Styles:

Methods can be called in two equivalent ways:

  1. Standard Function Syntax: Pass the object explicitly as the first argument: val = scaleLatitude(myObj, 3);
  2. Dot-Notation Syntax (Preferred): Call the method on the object itself: val = myObj.scaleLatitude(3); (In this style, the first object parameter obj is automatically mapped behind the scenes).

3. Arithmetic Operator Overloading

You can overload standard arithmetic symbols by defining methods matching special built-in names:

OperatorOverloaded Function NameDescription
+plus(o1, o2)Addition
-minus(o1, o2)Subtraction
*mtimes(o1, o2)Matrix Multiplication
.*times(o1, o2)Element-wise Multiplication
/mrdivide(o1, o2)Matrix Right Division

💻 Conceptual Code Demonstration

This code represents the complete structure of the class definition Waypoint.m:

classdef Waypoint
    properties
        latitude
        longitude
    end
    
    methods
        % 1. Constructor
        function obj = Waypoint(lat, lon)
            if nargin > 0 % Check if arguments were passed
                obj.latitude = lat;
                obj.longitude = lon;
            end
        end
        
        % 2. Member Method
        function r = scaleLatitude(obj, n)
            r = n * obj.latitude;
        end
        
        % 3. Overloaded '+' Operator
        function r = plus(o1, o2)
            r = Waypoint(o1.latitude + o2.latitude, ...
                         o1.longitude + o2.longitude);
        end
    end
end

Usage at command line:

% Create objects
pt1 = Waypoint(45.0, 30.0);
pt2 = Waypoint(10.0, 5.0);
 
% Method invocation formats
res1 = scaleLatitude(pt1, 2); % Standard
res2 = pt1.scaleLatitude(2);  % Dot-notation (preferred)
 
% Overloaded operator trigger
ptCombined = pt1 + pt2; % Internally calls plus(pt1, pt2)
fprintf('New Coordinates: Lat %.1f, Lon %.1f\n', ...
        ptCombined.latitude, ptCombined.longitude);