MATLAB Object-Oriented Programming

(Joyce) #1

classdef MyHandleClass < handle
...
end


Behavior of MATLAB Built-In Classes


MATLAB fundamental classes are value classes (numeric, logical, char, cell, struct,
and function handle). For example, if you create an object of the class int32 and make a
copy of this object, the result is two independent objects. When you change the value of a,
the value of b does not change. This behavior is typical of classes that represent values.


a = int32(7);
b = a;
a = a^4;


b
7


MATLAB graphics objects are implemented as handle objects because they represent
visual elements. For example, create a graphics line object and copy its handle to another
variable. Both variables refer to the same line object.


x = 1:10; y = sin(x);
l1 = line(x,y);
l2 = l1;


Set the properties of the line object using either copy of the handle.


set(l2,'Color','red')
set(l1,'Color','green')


get(l2,'Color')


ans =


0 1 0


Calling the delete function on the l2 handle destroys the line object. If you attempt to
set the Color property on the line l1, the set function returns an error.


delete(l2)
set(l1,'Color','blue')


Error using matlab.graphics.primitive.Line/set
Invalid or deleted object.


Comparison of Handle and Value Classes
Free download pdf