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.
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

I found that HANDLE is also a buit-in function of MATLAB but it is not documented! What’s the function of this function? How to use it? When should I use it?
Hi Junziyang-
handle is an undocumented feature related to handle graphics for internal use at the moment so we can refactor some of the internal underpinnings and maintain compatibility.
I doubt you should find a need for it.
–Loren
Do you think about a method to change the color (or any other property, like linewidth) of all the elements of a plot produce by a built-in function?
for instance there is no way to specify the linewidth of the boxplot function, so I use something like that:
figure;
X = randn(100,1);
plot(ones(size(X)),X,’.k’);
c = get(gca,’children’);
hold on
boxplot(X);
hold off
bpc = setdiff(get(gca,’children’), c);
set(bpc, ‘linewidth’,2, ‘color’, [0 .5 .5]);
but for functions like candle, it cannot work because candle is made with lines and patchs (and patch do not have a color property), so I use a switch/case control on the object type…
for candle it’s especially usefull because the original code (2006a) does not allow to change the color (it only changes the color of the patchs), cf:
http://en.literateprograms.org/Matlab_Financial_Toolbox
may be it is possible to ‘group’ handles like with something like ‘hggroup’ (but I did not had a lot of success with it) and then to change all properties at once?
thanks
charles
you did not write anything about database and misc datasources (as financial data providers) connection handles. they are very heterogeneous (oracle, bloomberg, datastream, etc).
rather than openning and closing those connections after each use, I choose to strore them into the get(0, ‘userdata’) property, they are in an hashtable indexed by any connection string that has been use and I close them before quiting matlab with a statement like your:
all(arrayfun(@fclose, fids)==0)
have you a point of view about that?
regards
charles
Personally I thinking storing information in ‘userdata’ is a poor idea. You’d be better off using a nested function and keeping a handle to the resource stored with the function handle. That way, you don’t stand a chance for userdata to get corrupted.
Loren,
Another good topic to cover. Although, I suspect you only scratched the surface.
How does a handle compare and contrast to a data structure?
Respected Loren,
I am receiving blog posts from Steve Eddins.
Recently i came to know about your blog posts in steve blogs.
But i am unable to subscribe to your blogs.
Kindly help me.
Kathirvel-
You should be able to subscribe in a similar way that you did for Steve’s. If you go to the right side in the blue area on my blog, you will see an area labeled Subscribe that has directions.
–Loren
I think that (online) documentation regarding handles related to plotting is quite poor and the handles get more complex if you do more complicated plottin. For instance, in a subplot with two plots, things can get really strange. If I try to access specifically the handle of the second subplot title, (figure.axes.title) I should get a cell array, but this only happens if I first try to get its parameters. I assume this is due to handlevisibility property set to off which effectively prevents me from accessing it.
So, how am I supposed to manipulate the title of the second (or nth) subplot? No problem with the first…