Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I mimic a button click in matlab

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.

like image 958
magu_ Avatar asked Jan 08 '23 00:01

magu_


2 Answers

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.

like image 64
bla Avatar answered Jan 17 '23 07:01

bla


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.

like image 44
Hoki Avatar answered Jan 17 '23 07:01

Hoki