Value classes Vs Handle classes - Matlab OOP 11 | applied electronics engineering

# Value classes Vs Handle classes - Matlab OOP 11

By Applied Electronics - Friday, February 10, 2017 No Comments
This Matlab Object Oriented Programming tutorial explains the basic difference between value and handle classes in Matlab.

We can classify Matlab classes in different ways. One way to classify the classes is user defined classes and builtin classes. The builtin classes are the 16 fundamental classes- logical, strings/characters, numeric(including unsigned and signed integers, single and double), table, cell, struct and handle. Another way to see these classes is to differentiate value classes and handle classes. The fundamental classes are value classes.

Before further reading you might want to see preceding Matlab Object Programming Tutorials.

### Value classes Vs Handle classes

The value classes objects are independent to each other. Consider that A is a value class object. Then you create another value class object B by copying A.  These two value classes objects are independent meaning that changing A will not effect B. In fact we do this all the time in Matlab. Consider the following:

A = 13;
B = A;
B = 14;
>> A

A =

13

>> B

B =

14


As you perhaps know the default data type is of type double. A double data type is one of the 16 fundamental class. In the above code, A is an object of class double with value assigned to it 13. The object B is created by copying it from A. So A and B are both objects of double value class.The following proves this.

>> class(A)

ans =

double

>> class(B)

ans =

double


A is 13 and B is made 14. Both are independent meaning that changing B or A does not effect either of them.

Handle class is different. If we create a handle object A from handle class and create another object B by copying A then A and B are same. This is because both A and B will refer to the same object in memory. For example plot is a handle class. Let A = plot(....) be a handle object. Then create another object B by assigning A to B, that is by using B = A. Now changing handle object B will also change handle object A. Or by changing the handle object A will also change B. Read the following code segment for illustration.

>> t = 0:0.1:2*pi;
>> y = sin(t);
>> A = plot(t,y);
>> B = A;
>> x = sin(2*t);
>> B = plot(t,x);
>> A =

handle to deleted Line

>> B

B =

Line with properties:

Color: [0 0.4470 0.7410]
LineStyle: '-'
LineWidth: 0.5000
Marker: 'none'
MarkerSize: 6
MarkerFaceColor: 'none'
XData: [1x63 double]
YData: [1x63 double]
ZData: [1x0 double]

Show all properties

>> A = B

A =

Line with properties:

Color: [0 0.4470 0.7410]
LineStyle: '-'
LineWidth: 0.5000
Marker: 'none'
MarkerSize: 6
MarkerFaceColor: 'none'
XData: [1x63 double]
YData: [1x63 double]
ZData: [1x0 double]

Show all properties

>> B

B =

Line with properties:

Color: [0 0.4470 0.7410]
LineStyle: '-'
LineWidth: 0.5000
Marker: 'none'
MarkerSize: 6
MarkerFaceColor: 'none'
XData: [1x63 double]
YData: [1x63 double]
ZData: [1x0 double]

Show all properties

>> A == B

ans =

1


The last line is to check whether A and B are equal. 1 means true.