Loren on the Art of MATLAB

Turn ideas into MATLAB

Note

Loren on the Art of MATLAB has been archived and will not be updated.

Handle This!

I recently got a great request from a reader to discuss the various kinds of entities in MATLAB attached to the word handle. I want to talk about the parallels and differences of three affecting most MATLAB users:

And, of course, there's also George Frideric Handel

To hear more, try this in MATLAB:

               load handel
               sound(y,Fs)

Contents

Similarities

In all three cases, the handle or identifier let's you refer to another entity and possibly change its state, without creating a new variant.

  • For files, this means that I can read part of a file and advance through it while always being able to refer to that particular file.
  • Similarly, for graphics handles, I can change properties associated with the objects being referred, e.g., their colors, without changing how I refer to the graphics object itself.
  • Finally, for function handles, I can refer to a particular function via its handle even when conditions, such as the path or current directory, change. Also, for handles to nested functions, I am able to change the state of the function handle's workspace without needing to create a new way to refer to that particular instance of a function.

Differences

  • File handles and handle graphics objects all have top-level ways to locate them, even if they aren't assigned a variable in your current workspace.

Let me start by exploring a little bit about files.

fid1 = fopen('cumtrapz.m');
fid2 = fopen('trapz.m');
fid3 = fopen('mean.m');
clear

Now find the open files even though the workspace is empty.

fids = fopen('all')
fids =
     3     4     5

I have to close the files one at a time. Successful closure returns a 0. Make sure they all succeed.

success = 0;
if all(arrayfun(@fclose, fids)==0)
    disp('Success, all files closed')
else
    disp Oops!
end
Success, all files closed

Prove that no files are open.

fids = fopen('all')
fids =
     []

I can also use probe and manipulate within an open file using fseek and ftell to change and query the position indicator in the file.

Clear the workspace again, and any possibly lurking figures.

Next I will explore more about graphics handles by creating a plot and finding the line contained in the figure.

clear all
close all
load handel
t = (0:(numel(y)-1))/Fs;
plot(t,y)
title Handel
xlabel Seconds
clear

Now let me find the line I've drawn. I'll start by getting the children of the root from handle value 0. Then I'll get the axes, and finally the line inside.

figs = get(0,'Children')
ax = get(figs, 'Children')
handelLine = get(ax,'Children')
figs =
     1
ax =
  160.0015
handelLine =
  161.0034

To prove it's the same line, let me change the color. Notice that the handle itself has not changed value but the color property associated with it did.

set(handelLine,'Color','g')
handelLine
handelLine =
  161.0034

For function handles, I get use the functions function to query the state, possibly contained, for example, in a nested workspace. I cannot, however, directly change the state. I must use the function handle itself or some other function handle that has access to the nested workspace in order to affect a change.

  • File handles and handle graphics objects all have ways to dispose of them. For files, I simply use fclose, and for graphics objects, I delete them.

For function handles, I must delete every possible variable and callback that points to the handle in question to be sure that everything associated with the function handle has been purged.

Comparisons

File identifiers, handles to graphics objects, and function handles all use similar nomenclature and they share a lot of functionality and interaction in common. File identifiers and graphics objects can have persistence beyond the lifetime of the variables and programs that create them, whereas function handles last only until the last reference to them disappears. Please add your thoughts to these comparisons.


Published with MATLAB® 7.2


  • print