如何模仿用户单击来调用 GUI 对象的回调函数?
我正在尝试以编程方式在 MATLAB 中创建一个单击事件,该事件将模仿用户单击 GUI 对象。该对象的回调函数是一个子函数,所以我不能直接调用它。但是,我可以从对象获取回调属性,该属性最终是一个包含以下内容的 3×1 元胞数组:
@uiBlockFn/callback_til [ 188.0011] [1x1 struct]
如何在代码中调用此回调函数,以便它模仿用户单击 GUI 对象时会发生什么?
I'm trying to programmatically create a click event in MATLAB that will mimic the user clicking on a GUI object. The callback function for the object is a subfunction, so I can't call it directly. However, I am able to get the callback property from the object, which ends up being a 3-by-1 cell array with the following contents:
@uiBlockFn/callback_til [ 188.0011] [1x1 struct]
How can I invoke this callback function in code such that it mimics what would happen when a user clicks the GUI object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您有一个带有句柄
hObject
的图形对象,并且您获得了该对象的回调,如下所示:正如您所提到的,您得到的元胞数组
callbackCell
最终是一个 3 元素元胞数组,第一个单元格中有一个 函数句柄另外两个中的其他数据细胞。当定义对象的 回调时作为元胞数组(就像您的情况一样),回调函数句柄(或字符串名称)存储在第一个单元格中,您想要传递给回调函数的其他输入参数位于其余单元格中。但是,当激活对象时调用此回调时,实际上 MATLAB 将在输入参数列表的开头自动插入 2 个附加参数。它们是:
hObject
:现在正在调用其回调的对象的句柄。eventData
:与用户激活事件相关的数据结构,通常只是空矩阵[]
(一些情况)。因此,如果您想模仿用户激活的对象的操作,您需要按如下方式调用回调函数(假设不需要事件数据):
Let's say you have a graphics object with handle
hObject
, and you got the callback for the object like so:As you mentioned, the cell array
callbackCell
that you get ends up being a 3 element cell array with a function handle in the first cell and other data in the other two cells. When the callback for an object is defined as a cell array (like it is in your case), the callback function handle (or string name) is stored in the first cell and additional input arguments you want passed to the callback function are in the remaining cells.However, when this callback is invoked when the object is activated, there will actually be 2 additional arguments automatically inserted by MATLAB at the beginning of the input argument list. These are:
hObject
: The handle to the object whose callback is now being called.eventData
: A structure of data related to the user-activated event, which is often just the empty matrix[]
(except in a few cases).So, if you want to mimic the action of the object being activated by the user, you would want to invoke your callback function as follows (assuming there is no event data needed):
这就是内置 hgfeval 函数的用途:
http://undocumentedmatlab.com/blog/hgfeval/
This is what the built-in hgfeval function is for:
http://undocumentedmatlab.com/blog/hgfeval/