Getting Matlab handles events or properties

余生颓废 提交于 2019-11-29 04:49:14

Under the hood, Handle Graphics (HG) are implemented using the undocumented UDD mechanism, not the usual classdef-style OOP exposed to the user.

That's why you cant directly use the meta.class system to get meta-information on such handles.

As you already found out on Yair Altman's blog, there are undocumented ways to listen to events:

fig = hg.figure(); plot(rand(100,1))
lh = handle.listener(fig, 'WindowButtonDownEvent',@(~,~)disp('clicked'));

If you already have an existing HG object handle (represented with a numeric handle), use handle to convert it to a UDD handle:

f = figure();
fig = handle(f);

and yes I know, the term handle is quite overloaded in MATLAB and may refer to many things

Werner

As I was improving my question, I managed to find an answer for this (unfortunately it seems that I haven't seem them before at my searchs, and what's worse, some of the links I've opened before…)

Here the undocummented matlab blog shows how to get the handles from a matlab handle object. And it seems that there was already a question about this made at 2011 about this issue here in stackoverflow, and properly answered by @gnovice. The answer is:

>> get(get(classhandle(handle(gcf)),'Events'),'Name')
ans = 
    'SerializeEvent'
    'FigureUpdateEvent'
    'ResizeEvent'
    'WindowKeyReleaseEvent'
    'WindowKeyPressEvent'
    'WindowButtonUpEvent'
    'WindowButtonDownEvent'
    'WindowButtonMotionEvent'
    'WindowPostChangeEvent'
    'WindowPreChangeEvent'

I still would like to call your attention to the FEX as another good solution that may give you better possibilities for working with the graphical components offered by matlab.

Usage example:

>> k=handle.listener(gcf,'WindowButtonMotionEvent','disp(''MOVEMENT DETECTED!!'')');   
>> MOVEMENT DETECTED!! % When you move the mouse on the figure
>> MOVEMENT DETECTED!!
>> MOVEMENT DETECTED!!
>> MOVEMENT DETECTED!!
>> MOVEMENT DETECTED!!
>> MOVEMENT DETECTED!!
>> delete(k)

Try using get:

fig = gcf();
get(fig)

I don't know how to do this. I can provide some sample code to demonstrate what I think is being asked. This is a relatively new (therefore unused) Matlab feature:

hh = handle(gca);
lsnr = addlistener(hh,'XLim','PreGet',@(~,~)disp('<<<Getting XLIM values>>>'))

To see the listener in action

>> get(hh,'XLim')
<<<Getting XLIM values>>>
ans =
     0     1

I think the question is how to get lsnr from gca if the value was not stored.

I can't find a way.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!