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?
A callback is a command that executes in response to some predefined user action, such as clicking on a graphics object or closing a figure window.
In computer programming, a callback is a piece of executable code that is passed as an argument to other code, which is expected to call back (execute) the argument at some convenient time. The invocation may be immediate as in a synchronous callback or it might happen at later time, as in an asynchronous callback.
Events are notices that objects broadcast in response to something that happens, such as a property value changing or a user interaction with an application program. Listeners execute functions when notified that the event of interest occurs.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With