# Handle Object Behavior in MATLAB

MATLAB is an object-oriented programming (OOP) language that allows us to create objects and define their behavior.

• In MATLAB, there is a data type called 'Handle' which is used to point an object. Handles in MATLAB also allows us to pass a function as an input argument of some other function.

• In MATLAB programming, the handles are passed by reference instead of value. Therefore, when any change is made in the object's property, it will reflect across all the references.

• The use of handles in a MATLAB program makes it more memory efficient because it does not require copying the entire data of the object.

Overall, the handle is a type of object in MATLAB programming which is used to store the reference to an object. Now, let us discuss the behavior of handle object.

## Copies of a Handle in MATLAB

When we create multiple copies of a handle in MATLAB, the copies of the handle will also point to the same object as the original handle.

### Example

Consider the following MATLAB code to understand this behavior of MATLAB handle object.

% MATLAB program to explain copies of handle object
% Define a function
function square = f(a)
square = a^2;
end

% Create a function handle
fun_handle = @f;

% Display the square of a number using the original function handle (@f)
fprintf('Square using original handle: %d', fun_handle(5));

% Create a copy of the function handle
copy_fh = fun_handle;

% Display the square of a number using the copied function handle
fprintf('Square using copied handle: %d', copy_fh(5));


### Output

Square using original handle: 25
Square using copied handle: 25


### Explanation

In this MATLAB code, we have first defined a function 'f' to calculate square of a number 'a'. Then, we create a function handle 'fun_handle' to call the function and display the result using this function handle.

After that, we create a copy of this function handle as 'copy_fh' and display the result using this copied function handle.

We can see that the output generated from the both function handle is the same. Hence, this explains that when a copied function handle is used, then it also points to the same object as the original function handle.

## Modify the Handles using Functions

In MATLAB, we can also change/modify the function handles using functions. In other words, we can utilize a function handle to change the objects directly. Thus, when we pass a function handle which is pointing to a function, then any modification that we do in the object inside the function will also affect the original object exist outside the function. This happens because we are dealing with the same object, just with a different name.

Overall, in MATLAB, when we alter an object using a function handle, then we are actually modifying the object that is pointed by this function handle.

### Example

Let us consider an example to understand this behavior of function handle in MATLAB.

% MATLAB code to modify handles using functions
% Define a function
function result = f(x)
result = (x.^2) / 2;
end

% Create a function handle
fh = @f;

% Specify values for which the sum to be calculated
a = [5, 3];

% Calculate the sum using the function handle
sum_result = sum(fh(a));

% Display the sum result
fprintf('Sum of the values: %f', sum_result);


### Output

Sum of the values: 17.000000


### Explanation

This code demonstrates that we can easily modify the function handle using a function. In this example, we have modified the function handle 'fh' by using the built-in function 'sum' to calculate the sum of two numbers.

## Determine Whether an Object is a Handle

In MATLAB, there is a built-in function 'isa' which is used to determine whether the specified object is a handle or not.

This function takes the following syntax:

isa(object_name, 'handle')


The output of this function is a Boolean value, i.e. True or False. If the specified object is a handle, then the function will return True otherwise False.

### Example

Let us consider an example to understand this behavior of the handle in MATLAB

% MATLAB code to check if an object is a handle
% Define a function
function result = f(x)
result = (x.^2);
end

% Create a function handle
a = @f;

% Create a variable
b = 200;

% Determine if the objects are a handle or not
fprintf('Determining for the object a:');
disp(isa(a, 'handle'));

fprintf('Determining for the object b:');
disp(isa(b, 'handle'));


### Output

Determining for the object a:  1
Determining for the object b:  0


### Explanation

Here, the value '1' for the object 'a' is representing that 'a' is a function handle which is pointing to a function 'f' as created by using '@f'. The second result, i.e. the value '0' for the object 'b' represents that 'b' is not a function handle, but it is a variable.

This is how we can check if the given object is a handle or not in MATLAB.

## Deleted Handle Objects

In MATLAB, if we delete an object pointed by a handle, in that case the handle that pointing the object can still present in the workspace. However, this handle becomes invalid or null, as the object that it is pointing is no longer present.

Therefore, deleting an object removes the object from the workspace, but it does not remove the handle pointing to the object.

### Example

Let us consider an example to understand this concept.

% MATLAB code to delete an object pointed by a handle
% Create axes to plot a graph
a = axes;

% Specify data to plot the graph
x = 0:.5:2;
y = 0: 0.2: 2;

% Plot data on the axes
plot(a, x, y);

% Delete the axes after plotting
delete(a);

% Determine if the handle still exists or not
fprintf('Does the handle 'a' still exist? ');
if exist('a', 'var')
disp('Yes');
else
disp('No');
end


### Output

Does the handle 'a' still exist? Yes


### Explanation

This code first creates an axes object using the 'axes' function and then plot the data specified by 'x' and 'y'. After that the 'delete' function deletes the axes. Next, it checks whether the handle 'a' pointing to the axes object exists or not. As in MATLAB, if the object is deleted, the handle pointing it can still exist. Hence, the output of this code will be 'Yes'.

## Conclusion

This is all about handle object behavior in MATLAB. In this tutorial, we explained the variable behavior of handle objects with the help of example programs.

Updated on: 07-Sep-2023

24 Views