Master Class on Object-Oriented Programming with MATLAB

Jul 10, 2024

Master Class on Object-Oriented Programming with MATLAB

Introduction

  • Presenter: Lauren Shore, MathWorks (30+ years of experience)
  • Objective: Introduction to object-oriented programming (OOP) in MATLAB
    • For MATLAB users needing to learn OOP
    • Users from other languages wanting to see OOP in MATLAB
  • Resources: Maintains a blog on MathWorks website - "The Art of MATLAB"

Agenda

  1. Why use OOP?
  2. Basic concepts and terminology
  3. How to create a class
  4. Making classes more robust
  5. Advanced features (overview only)

Example: Locating Balloons with Radar

  • Problem: Finding and locating blips from radar deployed on balloons
  • Scenario: Sensor array detects blips, computes angles of arrival over time

Progression of Programming Techniques

  1. Direct command line calculations (e.g., calculating interest)
  2. Using variables in a script
  3. Grouping related variables into structures
  4. Advanced techniques: Classes combining algorithms (methods) and data (properties)

Basic Concepts of OOP

  • Class: Blueprint for an object containing properties (data) and methods (behavior)
  • Object: Specific instance of a class
  • Properties: Data attributes of a class
  • Methods: Functions defining the behavior of a class
  • Example: Class Dog with properties (breed, age, color) and methods (eat, sit, bark).

Creating a Class in MATLAB

  • Basic Structure: Class definition, properties, methods
  • Example: Create a class Blip with properties aoa, range, and signal
  • Object Creation: b = Blip(), setting properties with b.aoa = -10, b.range = ...

Features of MATLAB Classes

  • Self-documenting: Methods and properties are easily accessible
  • Error Checking: Avoids mistakes like incorrect property names
  • Customization: Constructors for initializing objects with preset values
  • Indexing: Combination of array and dot notation
  • Encapsulation: Hide implementation details, control property access

Method Example

  • Define methods within a class to add functionality (e.g., function identify for Blip)
  • Use object properties within methods using obj.propertyName

Procedural vs. Object-Oriented Programming

  • Procedural: Focus on procedure and data separated, flexible but prone to errors
  • OOP: Combines data and behavior, more robust and error-resistant

Advanced Features: Encapsulation and Data Protection

  • Define public, private, and protected properties and methods
  • Prevent arbitrary changes to important properties (e.g., speed of light)
  • Use dependent properties and property validation for consistency

Inheritance

  • Inheritance: Creating a subclass that inherits properties and methods from a superclass
  • Example: MovingBlip inherits from Blip and adds deltaAOA
  • Allows code reuse and extends functionality without rewriting

Practical Demonstration

  • Scenario: Radar detecting multiple blips, some stationary and some moving
  • Create and manipulate objects, use methods to simulate radar detection

MATLAB OOP Features

  • Full support for OOP: class definitions, inheritance, method overloading
  • Static Methods, Validators, Handle Classes, Destructor Methods
  • Documentation and Examples: Extensive resources available on MathWorks website
  • Training: MATLAB training courses available for deeper learning

Additional Resources

  • MATLAB Expo 2020 on Demand: Technical talks on MathWorks products
  • Simulink Student Challenge: Competitions with prizes
  • MathWorks YouTube Channel: Subscribe for updates on events and tutorial videos

Conclusion

  • Summary: Review of key concepts in OOP and its implementation in MATLAB
  • Invitation: Engage with resources and further your learning in OOP with MATLAB