|On this page…|
In the MATLAB® language, every value is assigned to a class. For example, creating a variable with an assignment statement constructs a variable of the appropriate class:
>> a = 7; >> b = 'some string'; >> whos Name Size Bytes Class a 1x1 8 double b 1x11 22 char
Basic commands like whos display the class of each value in the workspace. This information helps MATLAB users recognize that some values are characters and display as text while other values might be double, single, or other types of numbers. Some variables can contain different classes of values like cells.
You can create your own MATLAB classes. For example, you could define a class to represent polynomials. This class could define the operations typically associated with MATLAB classes, like addition, subtraction, indexing, displaying in the command window, and so on. However, these operations would need to perform the equivalent of polynomial addition, polynomial subtraction, and so on. For example, when you add two polynomial objects:
p1 + p2
the plus operation would know how to add polynomial objects because the polynomial class defines this operation.
When you define a class, you overload special MATLAB functions (plus.m for the addition operator) that are called by the MATLAB runtime when those operations are applied to an object of your class.
See Class for Polynomials for an example that creates just such a class.
MATLAB classes use the following words to describe different parts of a class definition and related concepts.
Class definition — Description of what is common to every instance of a class.
Properties — Data storage for class instances
Methods — Special functions that implement operations that are usually performed only on instances of the class
Events — Messages that are defined by classes and broadcast by class instances when some specific action occurs
Attributes — Values that modify the behavior of properties, methods, events, and classes
Listeners — Objects that respond to a specific event by executing a callback function when the event notice is broadcast
Objects — Instances of classes, which contain actual data values stored in the objects' properties
Subclasses — Classes that are derived from other classes and that inherit the methods, properties, and events from those classes (subclasses facilitate the reuse of code defined in the superclass from which they are derived).
Superclasses — Classes that are used as a basis for the creation of more specifically defined classes (i.e., subclasses).
Packages — Folders that define a scope for class and function naming
These are general descriptions of these components and concepts. This documentation describes all of these components in detail.
This section discusses some of the basic concepts used by MATLAB classes.
A class is a definition that specifies certain characteristics that all instances of the class share. These characteristics are determined by the properties, methods, and events that define the class and the values of attributes that modify the behavior of each of these class components. Class definitions describe how objects of the class are created and destroyed, what data the objects contain, and how you can manipulate this data.
It sometimes makes sense to define a new class in terms of existing classes. This enables you to reuse the designs and techniques in a new class that represents a similar entity. You accomplish this reuse by creating a subclass. A subclass defines objects that are a subset of those defined by the superclass. A subclass is more specific than its superclass and might add new properties, methods, and events to those inherited from the superclass.
Mathematical sets can help illustrate the relationships among classes. In the following diagram, the set of Positive Integers is a subset of the set of Integers and a subset of Positive numbers. All three sets are subsets of Real numbers, which is a subset of All Numbers.
The definition of Positive Integers requires the additional specification that members of the set be greater than zero. Positive Integers combine the definitions from both Integers and Positives. The resulting subset is more specific, and therefore more narrowly defined, than the supersets, but still shares all the characteristics that define the supersets.
The "is a" relationship is a good way to determine if it is appropriate to define a particular subset in terms of existing supersets. For example, each of the following statements makes senses:
A Positive Integer is an Integer
A Positive Integer is a Positive number
If the "is a" relationship holds, then it is likely you can define a new a class from a class or classes that represent some more general case.
Classes are usually organized into taxonomies to foster code reuse. For example, if you define a class to implement an interface to the serial port of a computer, it would probably be very similar to a class designed to implement an interface to the parallel port. To reuse code, you could define a superclass that contains everything that is common to the two types of ports, and then derive subclasses from the superclass in which you implement only what is unique to each specific port. Then the subclasses would inherit all of the common functionality from the superclass.
A class is like a template for the creation of a specific instance of the class. This instance or object contains actual data for a particular entity that is represented by the class. For example, an instance of a bank account class is an object that represents a specific bank account, with an actual account number and an actual balance. This object has built into it the ability to perform operations defined by the class, such as making deposits to and withdrawals from the account balance.
Objects are not just passive data containers. Objects actively manage the data contained by allowing only certain operations to be performed, by hiding data that does not need to be public, and by preventing external clients from misusing data by performing operations for which the object was not designed. Objects even control what happens when they are destroyed.
An important aspect of objects is that you can write software that accesses the information stored in the object via its properties and methods without knowing anything about how that information is stored, or even whether it is stored or calculated when queried. The object isolates code that accesses the object from the internal implementation of methods and properties. You can define classes that hide both data and operations from any methods that are not part of the class. You can then implement whatever interface is most appropriate for the intended use.
The following examples illustrate some basic features of MATLAB classes.
Developing Classes — Typical Workflow — applies object-oriented thinking to a familiar concept to illustrate the process of designing classes.
Working with Objects in Functions — shows advantages of using objects to define certain operations and how smoothly object fit in a function-oriented workflow.
Class to Represent Structured Data — shows the application of object-oriented techniques to managing data.
Class to Implement Linked Lists — using a handle class to implement a doubly linked list.
The following references can help you develop a basic understanding of object-oriented design and concepts.
Shalloway, A., J. R. Trott, Design Patterns Explained A New Perspective on Object-Oriented Design.. Boston, MA: Addison-Wesley 2002.
Gamma, E., R. Helm, R. Johnson, J. Vlissides, Design Patterns Elements of Reusable Object-Oriented Software. Boston, MA: Addison-Wesley 1995.
Freeman, E., Elisabeth Freeman, Kathy Sierra, Bert Bates, Head First Design Patterns. Sebastopol, CA 2004.
See Wikipedia® :Object Oriented Programming