Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
464 views
in Technique[技术] by (71.8m points)

user interface - Getting Matlab handles events or properties

The question

How may I get the list of events and properties for a handle of double type, as a figure, axes?

The issue

Matlab documentation says to you to use the WindowButtonDownFcn, WindowButtonMotionFcn, and so on in order to listen to whatever happens at your interface. The issue is that this properties are very limited as the following fact:

Keeping Variables in Scope

When MATLAB evaluates function handles, the same variables are in scope as when the function handle was created. (In contrast, callbacks specified as strings are evaluated in the base workspace.) This simplifies the process of managing global data, such as object handles, in a GUI.

Yes, this is perfect, if you don't have to redefine, add, or remove callbacks from your ButtonDownFcn, because if you do so, you will lose the other function handles variable scopes, as you are declaring them at a new scope which may will certainly not contain your so needed variables.

So one way would be to listen to the events themselves, not to properties that are called when the events are actived, and by doing so, you will not have to bother to redeclare your ButtonDownFcn and how to keep your variables at scope, because the other solutions are very slow to implement!. If I could listen to the events directly, as I do with handle.listener or addlistener matlab listening tools, I would not have to bother with that.

One good approach already known

One of the best solutions it seems is this FEX, which empowers the weak matlab WindowButtonDownFcn, WindowButtonDownFcn and whatever properties "listeners" function matlab has, so that you can have any amounts of functions listening to changes at the your graphical interface without having to care if your other functions handles will lose their scope variables.

With this I don't need to get the matlab events as it wrappers everything for me. But it still amuses me that matlab lead your users to use a broken feature instead of documenting the better approach and lead people to wrap everything around so that they can use things as they should be.


Information that may be useful.

I know about the meta.class that will give me all the properties, events and so on that a class have. For one class I have that inherits from a handle:

>> EventMeta = ?Event
EventMeta = 

  class with properties:

                     Name: 'Event'
              Description: ''
      DetailedDescription: ''
                   Hidden: 0
                   Sealed: 0
                 Abstract: 0
          ConstructOnLoad: 0
         HandleCompatible: 1
          InferiorClasses: {0x1 cell}
        ContainingPackage: []
             PropertyList: [64x1 meta.property]
               MethodList: [29x1 meta.method]
                EventList: [2x1 meta.event]
    EnumerationMemberList: [0x1 meta.EnumeratedValue]
           SuperclassList: [1x1 meta.class]

with that meta I can get the EventList from my Event class, which are:

>> EventMeta.EventList.Name

ans =

attemptToClick


ans =

ObjectBeingDestroyed

Well, this is not that great thing in this case, since I have implemented it and I know the events it has because I have the source. The thing is, if I can get the metaclass of a figure (if that is possible), I could have access to its implemented Events if they are available at the matlab.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...