I was thinking of setting one function for multiple pushbuttons, They all do the same thing, but it has a different defining value. This is so that when one pushbutton is activated it does not get mixed up with the other pushbutton of the same function
Asked
Active
Viewed 1,779 times
0
-
does the function has to do something slightly different depending on which `pushbutton` was pressed ? – Hoki Jul 07 '15 at 16:12
-
Is this a programmatic GUI or a GUIDE GUI? – sco1 Jul 07 '15 at 16:45
-
sorry for an extremely late response but @Hoki yes exactly. – Tamfor Dulin Jul 13 '15 at 21:32
-
Also @excaza it is programmatic GUI. – Tamfor Dulin Jul 13 '15 at 21:34
2 Answers
1
See the documentation for callbacks. Callbacks accept two input arguments by default: the handle of the object that invoked the function and a structure of event data from the object, which may or may not be empty. You can use the String
or Tag
properties of your pushbutton to control behavior of your GUI based on what button is pressed using a single callback function. Consider the following example:
function testGUI
handles.mainwindow = figure();
handles.mytextbox = uicontrol( ...
'Style', 'edit', ...
'Units', 'normalized', ...
'Position', [0.15 0.80 .70 .10], ...
'String', 'No Button Has Been Pressed' ...
);
handles.button(1) = uicontrol( ...
'Style', 'pushbutton', ...
'Units', 'normalized', ...
'Position', [0.05 0.05 .30 .70], ...
'String', 'Button1', ...
'Callback', {@mybuttonpress,handles} ...
);
handles.button(2) = uicontrol( ...
'Style', 'pushbutton', ...
'Units', 'normalized', ...
'Position', [0.35 0.05 .30 .70], ...
'String', 'Button2', ...
'Callback', {@mybuttonpress,handles} ...
);
handles.button(3) = uicontrol( ...
'Style', 'pushbutton', ...
'Units', 'normalized', ...
'Position', [0.65 0.05 .30 .70], ...
'String', 'Button3', ...
'Callback', {@mybuttonpress,handles} ...
);
end
function mybuttonpress(src, ~, handles)
switch src.String
case 'Button1'
handles.mytextbox.String = 'Button 1 Has Been Pressed';
case 'Button2'
handles.mytextbox.String = 'Button 2 Has Been Pressed';
case 'Button3'
handles.mytextbox.String = 'Button 3 Has Been Pressed';
otherwise
% Something strange happened
end
end
Note that this requires MATLAB R2014b or newer in order to use the dot notation for accessing object properties. See this blog post for more information.

sco1
- 12,154
- 5
- 26
- 48
-
Sorry for late response but this is a great idea but what if I was infinitely creating a button and I wanted it to work with that. So for example, there are only one button and another button to create the same button as that one button. I just decide to create 50 of that button and I still want it to have the same functionality as this code but without the hassle of making 50 cases how would I do that? Because my idea was using `sprintf('Button_%s',[value on tag or string]);` – Tamfor Dulin Jul 13 '15 at 21:36
-
@TamforDulin I'm not sure I understand. I would suggest asking a new question with some additional details. – sco1 Jul 14 '15 at 11:39
-
I'm sorry. But I've created a function that creates any amount of pushbutton the user wishes to have. So it would have the similar function to what you created. But instead of actually limiting the numbers of buttons to 3. There would be any amount of pushbuttons linked to one function. But all the pushbuttons do the same thing but slightly different depending on the button pressed. – Tamfor Dulin Jul 14 '15 at 14:17
0
You can just define a generic function and call it from all of your push button callbacks

Shikhar Sharma
- 115
- 1
- 2
- 6