Skip to content

MATLAB - Object Oriented Programming

MATLAB - A Practical Guide to Object-Oriented Programming

Section titled “MATLAB - A Practical Guide to Object-Oriented Programming”

Object-Oriented Programming (OOP) is a powerful paradigm for designing software that is modular, reusable, and easy to maintain. In MATLAB, OOP allows you to create custom data types (objects) that bundle data (properties) and the functions that operate on that data (methods) into a single unit. This is ideal for building complex applications, custom toolboxes, or managing sophisticated data structures.

  • Encapsulation: Bundle data and functions together, hiding internal complexity. An object manages its own state.
  • Reusability: Define a class once and create many instances (objects) of it. Use inheritance to extend existing classes without rewriting code.
  • Maintainability: Code is more organized and modular, making it easier to debug and update.
  • Customization: Overload existing MATLAB functions (like plot) to work specifically with your custom objects.

You define a class in a file named ClassName.m. The fundamental structure is a classdef block.

Let’s build a simple, practical class to represent a thermometer. It will store the temperature in Celsius and have methods to get the temperature in Fahrenheit and to display its state. Create a file named SimpleThermometer.m.

Once the SimpleThermometer.m file is on your MATLAB path, you can create and use objects from it in the command window or a script.

Properties are the variables that store an object’s data. You can control their access:

  • Access = public: (Default) Can be accessed from anywhere.
  • Access = protected: Can be accessed only by methods of the class or its subclasses.
  • Access = private: Can be accessed only by methods of the class itself.

Methods are functions that belong to a class. The first argument to any method is always the object itself (conventionally named obj). The constructor is a special method with the same name as the class, used to create and initialize the object.

Inheritance allows a new class (subclass) to inherit properties and methods from an existing class (superclass). This is a cornerstone of code reuse. Let’s create a DigitalThermometer that inherits from SimpleThermometer and adds a battery level.

4. Handle vs. Value Classes (A Key MATLAB Concept)

Section titled “4. Handle vs. Value Classes (A Key MATLAB Concept)”

This is a critical distinction in MATLAB OOP:

  • Value Classes (Default): Behave like standard MATLAB arrays (e.g., numbers). When you copy a value object, you get a full, independent copy. Modifying the copy does not affect the original. Our SimpleThermometer is a value class.
  • Handle Classes: Inherit from the built-in handle class (classdef MyClass < handle). When you copy a handle object, you are only copying the reference (the handle). Both the original and the copy point to the same underlying data. Modifying the copy will affect the original. This is useful for objects that represent unique physical entities, like a file handle or a GUI window.