create a function to press button (instead of a function calling the callback function of the button)
7 views (last 30 days)
Show older comments
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
on 19 Jul 2013
Do the three button use the same callback button_callback with a different number of inputs?
More Answers (1)
Jan
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
See Also
Categories
Find more on Migrate GUIDE Apps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!