One function for an infinite amount of pushbuttons in matlab

1 view (last 30 days)
So I was trying to create something that is similar to this testGUI below. 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.
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
  2 Comments
Geoff Hayes
Geoff Hayes on 14 Jul 2015
Tamfor - what can you tell us about the pushbuttons? What does each one do when you press it? Are you creating some sort of calculator or what?
Tamfor Dulin
Tamfor Dulin on 14 Jul 2015
Well, it's supposed to just identify what it is. Just like in the code above.

Sign in to comment.

Accepted Answer

Sean de Wolski
Sean de Wolski on 14 Jul 2015
Edited: Sean de Wolski on 14 Jul 2015
You can compares src directly to a list of handles to identify the button used and then make your decision based on this:
function lotsofbuttons
n = 10;
pbs = gobjects(n,1); % Store pushbuttons here
for ii = 1:n;
pbs(ii) = uicontrol('Style','Pushbutton','Units','normalized','Position',rand(1,4)./2,'Callback',@TheCallback);
end
function TheCallback(src,~)
idx = find(pbs==src); % Compare list to source
disp(['Pushbutton ' num2str(idx)])
end
end

More Answers (1)

Steven Lord
Steven Lord on 14 Jul 2015
As it stands, the buttons and the callback function are very tightly coupled; whenever you need to add a new button, the callback function must also change. I would break this dependency by giving the button a little bit more information.
function createMultipleButtons(n)
L = linspace(0.1, 0.9, n+1);
if n > 1
d = 0.9*(L(2)-L(1)); % Leave a little gap between the buttons
else
d = 0.8;
end
for k = 1:n
uicontrol('Style', 'Pushbutton', ...
'Units', 'normalized', ...
'Position', [0.1 L(k) 0.5 d], ...
'Callback', @callbackFcn, ...
'UserData', k, ...
'String', sprintf('Button %d', k));
end
function callbackFcn(h, varargin)
buttonNumber = h.UserData; % For releases prior to R2014b, use GET here
fprintf('Button %d pressed!\n', buttonNumber);
The callback knows the message. That is its responsibility. The message has a "hole" in it, the %d in the FPRINTF call's format string. To fill this hole, the callback asks the button whose callback is executing what its number is. When the callback function executes, the handle of the object whose callback this is is passed into the function as its first input.
Knowing which number it is (and telling the callback when it asks) is the button's responsibility. As long as the button "knows how to answer the question when the callback asks", the callback function doesn't care and doesn't NEED to care how many buttons there are! In fact, by changing one line of code, I can make the buttons answer with the square of the order in which they were created.
'UserData', k^2, ...
There is no need to change the callback function to adapt to the new numbering scheme. I'm changing the button's information, not the callback's information, so I change it in the code that creates the button.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!