MATLAB Object-Oriented Programming

(Joyce) #1

Role of Classes in MATLAB


In this section...
“Classes” on page 3-2
“Some Basic Relationships” on page 3-4

Classes


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 text';
s.Name = 'Nancy';
s.Age = 64;
whos

whos
Name Size Bytes Class Attributes

a 1x1 8 double
b 1x9 18 char
s 1x1 370 struct

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 are double precision numbers, and so on. Some variables can
contain different classes of values like structures.

Predefined Classes

MATLAB defines fundamental classes that comprise the basic types used by the language.
These classes include numeric, logical, char, cell, struct, and function handle.

User-Defined Classes

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. These operations would need to perform the equivalent of polynomial addition,
polynomial subtraction, and so on. For example, when you add two polynomial objects:

3 MATLAB Classes Overview

Free download pdf