One function for an infinite amount of pushbuttons in matlab
이전 댓글 표시
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
Geoff Hayes
2015년 7월 14일
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
2015년 7월 14일
채택된 답변
추가 답변 (1개)
Steven Lord
2015년 7월 14일
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.
카테고리
도움말 센터 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!