MATLAB(매트랩)

Object-Oriented Programming in MATLAB

Model real-world objects and manage software complexity

Use Object-Oriented Programming to Model Real-World Objects

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.


Example Transmitter classes in a wireless communications application.

Example Transmitter classes in a wireless communications application.

MATLAB objects provide a well-defined interface that hides internal complexity.

Use Object-Oriented Programming to Manage Software Complexity

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.


The Components of a MATLAB Class

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:

  • Properties blocks define the properties that store data for each of the objects of the class 
  • Methods blocks contain a set of functions that define the operations that can be performed on each object of the class
  • Events blocks define messages that an object will send to other parts of an application when something changes in that object

The Components of a MATLAB Class

A sample MATLAB class definition.

MATLAB provides property/method attributes and validation syntax to enforce property types, sizes, and values.

MATLAB provides property/method attributes and validation syntax to enforce property types, sizes, and values.

Defining Properties and Methods

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.


Working with Objects

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.

Create and work with MATLAB objects using familiar syntax.

Create and work with MATLAB objects using familiar syntax.