How do I keep a handle and use FINDOBJ to get a handle again?

4 views (last 30 days)
Handles have to be one of the most frustrating data objects to deal with in MATLAB.
I have created a class in which I want to store a reference to a figure (a window) that has been created. While I can live with a scalar double returned by the gcf() function and saving that somewhere in my object - as a double and not as a handle - why does any effort to recover that handle using findobj(123.0045) or any other such function with a number passed to it appear to only return with an error telling me I have an invalid handle reference.
So, what the devil IS a valid reference to a handle? How can I keep a reference to it reliably without having to call gcf or gca every time I want a particular handle that works?
NOTE EDIT to title by Doug to reflect nature of actual question. Slight mods to text of question for clarity.

Accepted Answer

Matt Tearle
Matt Tearle on 17 Feb 2011
What Matt Fig said, plus: are you actually copying the number in (rather than saving it in a variable and passing the variable into the function)? Because that won't work for anything other than a figure handle. Figure handles are integers, but everything else is actually a hex reference (that appears as a double because, as the old joke goes, you can have any data type you want in MATLAB as long as it's a double -- no longer true, of course, but the remnants of that are seen in places, like HG). Anyway, the key point being that the handle isn't 123.0045 -- it's 123.0045blahblahblah. Any attempt to copy/paste the actual number will invariably get the floating point representation slightly off, and will therefore result in an incorrect hex reference, and that error message you love so much.
Bottom line, then: save handles as variables.
As Matt F alluded to in his answer, you can store handles as outputs from 99% of graphics function calls:
hf = figure;
ha = axes;
hp = plot(x,y,x,z);
etc. In this case, hf and ha will be scalar handles, hp will be a vector of 2 handles (one to each line object on the plot).
Use findobj to locate handles that you don't already have:
h = findobj('type','line')
And use the parent and children properties to navigate the hierarchy. For example, the above example could also be done:
hp = plot(x,y,x,z)
ha = get(hp(1),'parent') % parent of a line is the axes
hf = get(ha,'parent') % parent of the axes is the figure
  1 Comment
Chris Shannon
Chris Shannon on 18 Feb 2011
Thank you. I was in fact nesting the gcf function inside my object function call myObj.callMethod(gcf), and then things were blowing up when I tried to use the variable as a figure handle. Now that I do this:
figureHandle = gcf;
myObj.methodCall(figureHandle)
things work smoothly.
I'm coming to Matlab with a background in Java/C++ and Perl/Python/Ruby, and also R. I've seen whatever happesn to be on the LHS of an assignment operation change what a function returns, but this behavior in Matlab is pretty subtle. Is there a theoretical reason for why Matlab handles assignment of handles this way, or is this just one of those things that is just an unintended quirk of the language?

Sign in to comment.

More Answers (2)

Matt Fig
Matt Fig on 17 Feb 2011
The FINDOBJ function finds the handle which fits the property/value pairs passed to it. It looks like you are passing a handle (123.0045) to FINDOBJ, which is bizarre because you have the handle already - what is the point? This is equivalent to doing:
ax = gca % Get the handle to current axes.
ax_handle = findobj(ax) % Get the handle to current axes.
You can store any handle structure you want in the root userdata. This is accessible from anywhere. For example:
clear all,close all,clc % Don't do this if your stuff isn't saved!
S.fh = gcf;
S.ax = gca;
S.L(1) = plot(1:10)
hold on
S.L(2) = plot(rand(1,4));
set(0,'userdata',S)
Now from any workspace whatsoever, you can call
S = get(0,'userdata');
and use the handles. Just don't forget the update the userdata property if new handles are then added to S, or if an object is deleted.
  1 Comment
Chris Shannon
Chris Shannon on 18 Feb 2011
Thank you for the answer. It works now. I guess I'm just getting used to the quirks of the Matlab language.

Sign in to comment.


Jiro Doke
Jiro Doke on 17 Feb 2011
What Matt and Matt said. Plus, I would emphasize again:
Never ever ever use the actual number to refer to the handle. Instead, save the scalar double number as a variable and just use the variable in the rest of your code.
  1 Comment
Chris Shannon
Chris Shannon on 18 Feb 2011
Yep, you are, of course right. I've duly corrected my code and it works now. Thank you.

Sign in to comment.

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!