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

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

问题描述

我如何获取双精度类型句柄的事件和属性列表,如 figureaxes?

Matlab 文档要求您使用 WindowButtonDownFcnWindowButtonMotionFcn 等来收听界面上发生的任何事情.问题是这个属性非常有限,因为 以下事实:

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:

将变量保持在范围内

当 MATLAB 计算函数句柄时,相同的变量在范围与创建函数句柄时一样.(相反,回调指定为字符串在基础工作区中进行评估.)这简化了管理全局数据的过程,例如对象句柄,在 GUI 中.

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.

是的,这是完美的,如果您不必从 ButtonDownFcn 重新定义、添加或删除回调,因为如果这样做,您将失去其他函数句柄变量范围,因为你在一个新的范围内声明它们,可能肯定不会包含你需要的变量.

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.

因此,一种方法是监听事件本身,而不是在事件激活时调用的属性,这样做,您不必费心重新声明 ButtonDownFcn 以及如何将变量保持在范围内,因为其他解决方案实施起来非常缓慢!.如果我可以像使用 handle.listeneraddlistener matlab 监听工具那样直接监听事件,我就不必为此烦恼了.

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.

似乎最好的解决方案之一是 this FEX,它赋予弱 matlab WindowButtonDownFcnWindowButtonDownFcn 和任何属性listeners"函数 matlab 的能力,这样您就可以在图形界面上拥有任意数量的函数来监听变化,而无需关心你的其他函数句柄是否会丢失它们的范围变量.

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.

有了这个,我不需要获取 matlab 事件,因为它为我包装了所有内容.但是,matlab 引导您的用户使用损坏的功能而不是记录更好的方法并引导人们将所有内容包装起来,以便他们可以按应有的方式使用东西,这仍然让我感到有趣.

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.

我知道 meta.class 这将为我提供一个类所具有的所有属性、事件等.对于我有一个继承自 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]

使用该元数据,我可以从我的 Event 类中获取 EventList,它们是:

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

>> EventMeta.EventList.Name

ans =

attemptToClick


ans =

ObjectBeingDestroyed

好吧,在这种情况下,这并不是一件好事,因为我已经实现了它并且我知道它所具有的事件,因为我有源代码.问题是,如果我可以获得 figure 的元类(如果可能的话),我可以访问其实现的 Events(如果它们在 matlab 上可用).

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.

推荐答案

在后台,Handle Graphics (HG) 是使用未记录的 UDD 机制,而不是向用户公开的通常的 classdef 风格的 OOP.

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

这就是为什么您不能直接使用 meta.class 系统来获取有关此类句柄的元信息.

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

正如您已经在 Yair Altman 的博客 上发现的,存在未记录的监听事件的方法:

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'));

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

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);

是的,我知道,术语 handle 在 MATLAB 中非常重载,可能指代很多东西

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

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

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