4

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.

magu_
  • 4,766
  • 3
  • 45
  • 79

2 Answers2

8

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.

bla
  • 25,846
  • 10
  • 70
  • 101
  • Thanks, this did the trick, with mousePress / mouseRelease its possible. A shame that there is no event to notify or similar. – magu_ Jul 07 '15 at 23:18
  • Always liked this trick. +1 @magu_ see [here](http://stackoverflow.com/a/27933690/2778484) and [here](http://stackoverflow.com/q/10033078/2778484) for some interesting examples. – chappjc Jul 08 '15 at 16:52
  • please note that this will also be executed outside the matlab environment – user2305193 Sep 08 '17 at 12:59
1

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.

Hoki
  • 11,637
  • 1
  • 24
  • 43
  • Thank you for your answer, I do have the handle of the button, the problem is that the callback function itself calls gbco. Sorry for not making this clear. – magu_ Jul 07 '15 at 22:53
  • @magu_, Actually you did make it clear, it's me who got confused on the ways to override the callback input. It would help if you could post a bit of the code which uses `gcbo`, so we can see if there are ways around your problem. – Hoki Jul 07 '15 at 23:04
  • That's a nice offer but the problem is as follows: The code is written/maintained by another person but I have to use it. Unfortunately the only interface I have for it is the gui with its buttons without actually changing the code. – magu_ Jul 07 '15 at 23:21