Object-Oriented Programming in MATLAB
Model real-world objects and manage software complexity
Model real-world objects and manage software complexity
Object-oriented programming is a design approach that enables you to programmatically define structures called objects that combine data (properties) together with functions that operate on that data (methods). In MATLAB®, you can create objects that model the behavior of devices and systems in the real world. Those objects can then be used as building blocks in applications used to simulate and analyze complex systems.
Tutorial
Using object-oriented programming in MATLAB, you can manage software complexity by organizing your code into logical components that are easier to maintain and extend. You can avoid code duplication by creating reusable objects with well-defined interfaces that hide the complexity of the underlying code. Furthermore, your objects can evolve and change over time without introducing incompatibilities in client code.
A MATLAB class contains a blueprint or set of instructions used to build a specific type of object. Class definitions start with the classdef keyword and have three major components:
Learn more
When defining a class, you can specify attributes to control how your class properties and methods behave and how they are accessed from outside the object. For example, properties and methods can be public, private, or protected. When specifying properties, you can use validation syntax to avoid writing code for error checking.
You can create objects using a special method called the class constructor. Calling the constructor is like calling any MATLAB function and can be used to create a single object or an array of objects. You can access object properties just like you would access the fields of a struct. Object methods are called just like ordinary MATLAB functions.
MATLAB objects have unique features relative to other languages. For example, you can modify a class at any time and objects of that class will update immediately. In addition, MATLAB manages the lifecycle of objects without requiring any explicit memory allocation or deallocation and without the type of non-deterministic garbage collection used in some other languages.