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

前端 未结 2 1953
夕颜
夕颜 2020-12-19 07:58

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 subfunct

2条回答
  •  囚心锁ツ
    2020-12-19 08:42

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

提交回复
热议问题