开发者

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

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 i开发者_JAVA百科n code such that it mimics what would happen when a user clicks the GUI object?


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜