How can I mimic a user click to invoke a callback function for a GUI object?

别来无恙 提交于 2019-11-28 03:59:48

问题


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?


回答1:


Let's say you have a graphics object with handle hObject, and you got the callback for the object like so:

callbackCell = get(hObject,'Callback');

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

callbackCell{1}(hObject,[],callbackCell{2:end});



回答2:


This is what the built-in hgfeval function is for: http://undocumentedmatlab.com/blog/hgfeval/



来源:https://stackoverflow.com/questions/5798109/how-can-i-mimic-a-user-click-to-invoke-a-callback-function-for-a-gui-object

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