How do I mimic a button click in matlab?
Simply excecuting the callback function doesn't work since within its callback it uses the gcbo command and I cannot alter the excecuting function. I furthermore would not like to shadow gcbo for obvious reasons.
In case it matters I look for a solution which works on matlab R2012a.
you can try calling the java.awt.Robot class, for example.
robot = java.awt.Robot;
pause(2) % wait some time
robot.keyPress (java.awt.event.KeyEvent.VK_ENTER); % press "enter" key
robot.keyRelease (java.awt.event.KeyEvent.VK_ENTER); % release "enter" key
read more about GUI automation using a Robot here...
I'm not sure this will work on Matlab R2012a, but it does on later versions.
gcbo
only contain the button handle. If you have (or can retrieve/find) the button handle, just call the function callback with the button handle as first argument and an empty variable as the second argument.
something looking like that:
button_callback( buttonHandle , [] ) ;
The callback will not make any difference between gcbo
or the button handle and will function exactly the same.
If you do not have the button handle in the first place, you can try to find it with findobj
:
buttonHandle = findobj(gcf,'Style','PushButton','String','The Button Text')
Depending on how the GUI was built/defined, then handle visibility may not be immediately apparent, in which case you can search deeper with findall
:
buttonHandle = findall(gcf,'Style','PushButton','String','The Button Text')
or may be the handle was nicely saved in the guidata structure:
handles = guidata(gcf) ;
and search the structure for what may be your button handle.
Note: in the last 3 examples above, make sure the GUI figure which contains the button has the focus before you call gcf
, or better yet, replace gcf
by the actual figure handle.
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