Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
The 'handle'-function

Subject: The 'handle'-function

From: Øyvind

Date: 19 Jan, 2010 15:32:04

Message: 1 of 7

I have been using the undocumented 'handle' function to turn handle graphics handles into -- well I am not quite certain what they are, but they simplify programming callbacks for gui components. For example, you can directly access properties using 'dot'-notation rather than the get/set interface.

Now it turns out that I need to get back the original numerical handle for use with functions like 'cla' or 'hold'. Anyone have any idea how this can be done?

Subject: The 'handle'-function

From: Jos (10584)

Date: 19 Jan, 2010 15:52:02

Message: 2 of 7

"Øyvind " <oyvist@gmail.com> wrote in message <hj4j9k$is1$1@fred.mathworks.com>...
> I have been using the undocumented 'handle' function to turn handle graphics handles into -- well I am not quite certain what they are, but they simplify programming callbacks for gui components. For example, you can directly access properties using 'dot'-notation rather than the get/set interface.
>
> Now it turns out that I need to get back the original numerical handle for use with functions like 'cla' or 'hold'. Anyone have any idea how this can be done?

You can use FINDOBJ, like this:

h = plot(rand(1,10),'b.-') ; pause ;
set(h,'color',[1 0 0]) ; pause ;
HC = handle(h) ;
HC.marker = 's' ; pause ;
h2 = findobj(HC) ;
set(h2,'color',[0 1 0]) ; pause ;

h2 is the numeric handle you need.

hth
Jos

Subject: The 'handle'-function

From: Yair Altman

Date: 19 Jan, 2010 17:17:05

Message: 3 of 7

"Øyvind " <oyvist@gmail.com> wrote in message <hj4j9k$is1$1@fred.mathworks.com>...
> I have been using the undocumented 'handle' function to turn handle graphics handles into -- well I am not quite certain what they are, but they simplify programming callbacks for gui components. For example, you can directly access properties using 'dot'-notation rather than the get/set interface.
>
> Now it turns out that I need to get back the original numerical handle for use with functions like 'cla' or 'hold'. Anyone have any idea how this can be done?

h = figure(...); % or uicontrol or axes or...
hh = handle(h); % or handle(h,'CallbackProperties') for better callback handling
dhh = double(hh);

So simple...

Yair Altman
http://UndocumentedMatlab.com

Subject: The 'handle'-function

From: Øyvind

Date: 19 Jan, 2010 17:28:04

Message: 4 of 7

"Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message <hj4peh$1rm$1@fred.mathworks.com>...
> "Øyvind " <oyvist@gmail.com> wrote in message <hj4j9k$is1$1@fred.mathworks.com>...
> > I have been using the undocumented 'handle' function to turn handle graphics handles into -- well I am not quite certain what they are, but they simplify programming callbacks for gui components. For example, you can directly access properties using 'dot'-notation rather than the get/set interface.
> >
> > Now it turns out that I need to get back the original numerical handle for use with functions like 'cla' or 'hold'. Anyone have any idea how this can be done?
>
> h = figure(...); % or uicontrol or axes or...
> hh = handle(h); % or handle(h,'CallbackProperties') for better callback handling
> dhh = double(hh);
>
> So simple...
>
> Yair Altman
> http://UndocumentedMatlab.com

Wow. Indeed. Thanks.

Doesn't work with my own classes derived from the handle class, unfortunately, but this was what I needed now.

Subject: The 'handle'-function

From: Jos (10584)

Date: 19 Jan, 2010 19:34:22

Message: 5 of 7

"Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message <hj4peh$1rm$1@fred.mathworks.com>...
 * snip *

> h = figure(...); % or uicontrol or axes or...
> hh = handle(h); % or handle(h,'CallbackProperties') for better callback handling
> dhh = double(hh);
>
> So simple...

Indeed so simple. Thanks Yair!

Jos

Subject: The 'handle'-function

From: us

Date: 19 Jan, 2010 20:14:04

Message: 6 of 7

"Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message <hj4peh$1rm$1@fred.mathworks.com>...
> "Øyvind " <oyvist@gmail.com> wrote in message <hj4j9k$is1$1@fred.mathworks.com>...
> > I have been using the undocumented 'handle' function to turn handle graphics handles into -- well I am not quite certain what they are, but they simplify programming callbacks for gui components. For example, you can directly access properties using 'dot'-notation rather than the get/set interface.
> >
> > Now it turns out that I need to get back the original numerical handle for use with functions like 'cla' or 'hold'. Anyone have any idea how this can be done?
>
> h = figure(...); % or uicontrol or axes or...
> hh = handle(h); % or handle(h,'CallbackProperties') for better callback handling
> dhh = double(hh);
>
> So simple...
>
> Yair Altman
> http://UndocumentedMatlab.com

although only semi-undocumented...

     lh=handle(line(1:10,rand(1,10)));
     fh=handle(gcf);
     ah=handle(gca);
     fj=fh.java;
     aj=ah.java;
     lj=lh.java;
% now, open up a whole new world (of undocs)...
% eg,
     methods(fj) % <- or methods(fj,'-full')
     methods(aj)
     methods(lj)
% and, eg,
     co=lj.getColor
     methods(co)
     co.setBlue(0);
     lj.setColor(co);
     lj.setLineStyle(1); % <- makes it easier to cycle through lists...
     lj.setMarker(5);

just another METHOD...
us

Subject: The 'handle'-function

From: Yair Altman

Date: 19 Jan, 2010 21:54:04

Message: 7 of 7

"us " <us@neurol.unizh.ch> wrote in message <hj53qc$aq7$1@fred.mathworks.com>...
> "Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message <hj4peh$1rm$1@fred.mathworks.com>...
> > "Øyvind " <oyvist@gmail.com> wrote in message <hj4j9k$is1$1@fred.mathworks.com>...
> > > I have been using the undocumented 'handle' function to turn handle graphics handles into -- well I am not quite certain what they are, but they simplify programming callbacks for gui components. For example, you can directly access properties using 'dot'-notation rather than the get/set interface.
> > >
> > > Now it turns out that I need to get back the original numerical handle for use with functions like 'cla' or 'hold'. Anyone have any idea how this can be done?
> >
> > h = figure(...); % or uicontrol or axes or...
> > hh = handle(h); % or handle(h,'CallbackProperties') for better callback handling
> > dhh = double(hh);
> >
> > So simple...
> >
> > Yair Altman
> > http://UndocumentedMatlab.com
>
> although only semi-undocumented...
>
> lh=handle(line(1:10,rand(1,10)));
> fh=handle(gcf);
> ah=handle(gca);
> fj=fh.java;
> aj=ah.java;
> lj=lh.java;
> % now, open up a whole new world (of undocs)...
> % eg,
> methods(fj) % <- or methods(fj,'-full')
> methods(aj)
> methods(lj)
> % and, eg,
> co=lj.getColor
> methods(co)
> co.setBlue(0);
> lj.setColor(co);
> lj.setLineStyle(1); % <- makes it easier to cycle through lists...
> lj.setMarker(5);
>
> just another METHOD...
> us

Urs - Readers might be confused by this to think that java(handle(h)) (or the equivalent hh.java) returns a reference to the Java component that underlies the Matlab one. In fact, it only returns a Javabean adapter to the Matlab object, that basically exposes the same functionality the pure Matlab handle.

To get the real Java object, which is *MUCH* more customizable than the Matlab version, you need to traverse the JavaFrame's hierarchy, or use the FindJObj utility that does it for you: http://www.mathworks.com/matlabcentral/fileexchange/14317-findjobj-find-java-handles-of-matlab-graphic-objects

Compare the following:

h=uicontrol; % a simple button

% BeanAdapter version:
hj1 = java(handle(h));
disp(hj1.class); % => uicontrolBeanAdapter0
m1=methods(hj1); % => 140 methods
p1=fieldnames(get(hj1)); % => 52 properties

% Java HG peer version:
hj2 = findjobj(h);
disp(hj2.class); % => javahandle_withcallbacks.com.mathworks.hg.peer.PushButtonPeer$1
m2=methods(hj2); % => 347 methods
p2=fieldnames(get(hj2)); % => 152 properties

Note that (unfortunately) Java peers are only available for uicomponents and similar controls - not for axes components.

Yair Altman
http://UndocumentedMatlab.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us