获取Matlab处理事件或属性 [英] Getting Matlab handles events or properties

查看:225
本文介绍了获取Matlab处理事件或属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题



如何获取double类型句柄的事件和属性列表,作为 / code>,



问题



Matlab文档告诉您,使用 WindowButtonDownFcn WindowButtonMotionFcn 等去听你接口上发生的一切。问题是这个属性非常有限,因为以下事实



在范围内保留变量


当MATLAB评估函数句柄时,相同的变量在
范围内,就像创建函数句柄一样。
(相反,回调
指定为字符串在基本工作空间中进行评估。)
简化了管理全局数据的过程,例如GUI中的对象
句柄。


是的,这是完美的,如果您不必重新定义,添加或删除ButtonDownFcn 中的回调,因为如果这样做,您将失去其他功能处理变量范围,因为您在新的范围内声明它们,可能肯定不会包含你所需要的变量。



所以一种方式是听事件本身,而不是在事件被激活时调用的属性,而通过这样做,你不必再重新声明您的ButtonDownFcn以及如何将变量保留在范围内,因为其他解决方案实施起来非常慢!。如果我可以直接听事件,就像使用 handle.listener addlistener matlab监听工具一样,我会



已知一种很好的方法



最好的解决方案之一是这个FEX ,它赋予弱matlab WindowButtonDownFcn和/或code> WindowButtonDownFcn 和任何属性监听器函数matlab具有,以便您可以有任意数量的函数监听您的图形界面上的更改,没有必须关心你的其他功能处理是否会丢失它们的范围变量。



这样我就不需要得到matlab事件,因为它包装了我的一切。但是,仍然让我感到欣慰的是,Matlab引导用户使用破坏的功能,而不是记录更好的方法,并引导人们将所有内容包起来,以便他们可以使用它们应该的东西。






可能有用的信息。



我知道 meta.class 这将给我所有的类,属性,事件等。对于一个类,我从句柄继承

 > > EventMeta =?Event 
EventMeta =

具有属性的类:

名称:'Event'
描述:''
DetailedDescription:' '
隐藏:0
密封:0
摘要: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]

与该元我可以得到EventList来自我的事件类,它们是:

 >> EventMeta.EventList.Name 

ans =

attemptToClick


ans =

ObjectBeingDestroyed $ b $嗯,这不是一件好事,因为我已经实现了它,我知道它有的事件因为我有来源。事情是,如果我可以得到一个数组元数据(如果可能的话),我可以访问其实现的事件如果它们在matlab上可用。

解决方案

在引擎盖下,处理图形(HG)是使用未记录的 UDD 机制,而不是通常的 classdef -style OOP暴露给用户。



这就是为什么你不能直接使用 meta.class 系统来获取元信息在这样的句柄上。



如你已经在 Yair Altman的博客,有无证件的方式来听事件:

  fig = hg。数字(); plot(rand(100,1))
lh = handle.listener(fig,'WindowButtonDownEvent',@(〜,〜)disp('clicked'));

如果您已经有一个现有的HG对象句柄(用数字句柄表示),请使用 handle 将其转换为UDD句柄:

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

是的,我知道,术语 handle 在MATLAB中相当重载,可能会引用很多东西


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.

解决方案

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

这篇关于获取Matlab处理事件或属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆