Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

create a function to press button (instead of a function calling the callback function of the button)

Asked by Gaëlle on 19 Jul 2013

Hi,

First of all sorry for the previous post that contains exactly this message. I did not pay attention and wrote it in the wrong section.

I made a GUI containing several buttons, each of them calling a specific callback with a bunch of arguments. For example, a button named 'my button' using two arguments arg1 and arg2 is created that way:

Positionmybutton=[0.1 0.1 0.1 0.1];
uicontrol(fh_gui,...
    'units', 'normalized','Position',Positionmybutton,...
    'String','my button',...
    'Callback',{@button_callback,arg1,arg2});

I can very easily find the object 'my button' since I know its position in the figure (use of findobj).

Remind now that I have several buttons (e.g. button1, button2, button3), each using a various number of arguments. My aim is to create a general push button 'general_button' which will act as if the user presses on multiple buttons (e.g. pressing on general_button = pressing on button2 and button3). However, I do not know how to deal with the button handles so that I do not have to pass every arguments that button2 and button3 would need as argument of the callback for general_button. I hope the following example will explain the problem itself.

Lets imagine that the the buttons are created that way:

uicontrol(fh_gui,...
    'Position',Positionbutton2,...
    'String','button2',...
    'Callback',{@button_callback, arg1_but2, arg2_but2});
uicontrol(fh_gui,...
    'Position',Positionbutton3,...
    'String','button3',...
    'Callback',{@button_callback, arg1_but3, arg2_but3, arg3_but3});

The general button would be:

    uicontrol(fh_gui,...
        'Position',Positionbuttongeneral,...
        'String','button general',...
        'Callback',{@button_callback, ...
                arg1_but2, arg2_but2...
                arg1_but3, arg2_but3, arg3_but3});

All the arguments of button2 and 3 are passed so that the callback of each button can be called with the correct arguments. Do they exist in the handle so that I simply have to pass the handles (one per button) instead of all the arguments ? Or more simply, is there a way to 'push' a button without the user pressing on it?

Thank you very much for your answers, Gaelle

1 Comment

Jan Simon on 19 Jul 2013

Do the three button use the same callback button_callback with a different number of inputs?

Gaëlle

Products

No products are associated with this question.

2 Answers

Answer by Gaëlle on 19 Jul 2013
Edited by Gaëlle on 19 Jul 2013
Accepted answer

Hi,

Thank you for your answer. I mainly needed the trick to get the callback name and arguments of a button, using

cb  = get(handles.button, 'Callback');

Now I would like to call the callback specified in the handles, for example:

   cb{1} = @gui_clusterGC2/tbutton_ISILFD

using the arguments in cb{2:end}. Is there a quick way to do this? -> I guess I can simply do:

cb{1}([],[],cb{2:end})

This works :)

Thank you!

0 Comments

Gaëlle
Answer by Jan Simon on 19 Jul 2013

A magic pressing of the button is possible by using the Java Robot, see e.g.<http://www.mathworks.com/matlabcentral/fileexchange/28357-jmouseemu-mouse-emulator-v2-2> .

But it would be cleaner to call the function directly and collect the inputs explicitly:

handles.button2 = uicontrol(fh_gui,...
  'Position',Positionbutton2,...
  'String','button2',...
  'Callback',{@button_callback, arg1_but2, arg2_but2});
handles.button3 = uicontrol(fh_gui,...
  'Position',Positionbutton3,...
  'String','button3',...
  'Callback',{@button_callback, arg1_but3, arg2_but3, arg3_but3});
handles.buttonGeneral = uicontrol(fh_gui,...
      'Position',Positionbuttongeneral,...
      'String','button general',...
      'Callback',@button_callback_General);
guidata(fh_gui, handles);
function button_callback_General(ObjectH, EventData)
handles = guidata(ObjectH);
cb2  = get(handles.button2, 'Callback');
cb3  = get(handles.button3, 'Callback');
args = cat(2, cb2(2:end), cb3(2:end))
button_callback(ObjectH, EventData, args{:});

Now changes in the callbacks of Button2 and Button3 are automatically considered, when the "general" Button is activated.

0 Comments

Jan Simon

Contact us