Open In App

Handle Object Behavior in MATLAB

Improve
Improve
Like Article
Like
Save
Share
Report

Handles in MATLAB are data type that points to an object. The handles can be used to point to an object from different references or to pass a function to another function as an input argument.

We shall not explain the creation and usage of MATLAB handles, as they are out of this article’s scope. However, MATLAB handles have some special properties that we shall discuss in this article.

Copies of a MATLAB handle

When a copy of a handle object is created, it still points to the same object as the original handle. See the following example to understand it better.

Example 1:

Matlab




% Code
fh = @fun;
fprintf('First handle: ')
disp(fh(3))
  
%creating copy of the handle fh
h=fh;
fprintf('Copied handle: ')
disp(h(2))
  
%function
function result = fun(x)
result = x^3;
end


Output:

 

As can be seen here, the copied handle and its original handle, both point to the same object, the function fun. 

Modification of handles using functions

MATLAB allows to modification of the handles as arguments to functions, which directly accesses the object referred to by the function. 

Example 2:

Matlab




% Code
h = @fun;
integration = integral(h,0,3)
  
%function
function result = fun(x)
    result = (x.^3)/4;
end


Now, here the function handle object h is modified by the inbuilt function integral as a vector to calculate the numerical integration in the given range.

Output:

 

Deletion of handle object

When the object that a handle refers to is deleted, the handle can still exist but, it will not point to anything. In a way, it will become a null handle.  In the following example, we create a handle for an axes object and then display the properties of the handle after deleting the object, to verify whether the handle exists or not.

Example 3:

Matlab




% Code
ax = axes;
x=0:.3:3;
plot(ax,x,x)


Output:

 

As we can see, ax points to our axes object. Now, we shall delete the handle. And as explained above, it will delete the object that the handle refers to instead of the handle itself.

Example 4: 

Matlab




% Code
ax = axes;
x=0:.3:3;
plot(ax,x,x)
delete(ax)
%checking whether the handle still exists or not
whos("ax")


Output:

 

As can be seen, the handle still exists however, the object it refers to is deleted.

Checking whether an object is a handle or not

We can check whether an object is a handle or not by using the is a() function.

Syntax

isa(object-name, ‘handle’)

it is a Boolean function and returns true if the object is a handle. See the example below to understand.

Example 5:

Matlab




% Code
ax = axes;
x=22;
fprintf('checking for object handle ax:')
disp(isa(ax,'handle'))
fprintf('checking for some variable x: ')
disp(isa(x,'handle'))


In this example, we create an axes object and a variable. Then we check with is a() for the object handle. The output will be in Boolean as follows.

Output: 

 

As it can be seen, ax is a handle however, x is not. 



Last Updated : 04 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads