HI,
I have two radio buttons in GUI. I want to make that only one button can be pressed at a time. what is the best way do it?
thanks,

댓글 수: 1

Andy
Andy 2015년 4월 18일
A Button Group panel is the best way make button selections exclusive. Here is a tutorial on how to do it: https://codemusician.wordpress.com/2013/03/15/gui-tutorial-how-to-create-and-use-radio-button-groups-in-matlab/

댓글을 달려면 로그인하십시오.

 채택된 답변

Honglei Chen
Honglei Chen 2012년 7월 10일
편집: John Kelly 2015년 2월 26일

0 개 추천

추가 답변 (3개)

Jan
Jan 2012년 7월 10일

7 개 추천

I prefer to control this manually:
handles.FigureH = figure;
handles.radio(1) = uicontrol('Style', 'radiobutton', ...
'Callback', @myRadio, ...
'Units', 'pixels', ...
'Position', [10, 10, 80, 22], ...
'String', 'radio 1', ...
'Value', 1);
handles.radio(2) = uicontrol('Style', 'radiobutton', ...
'Callback', @myRadio, ...
'Units', 'pixels', ...
'Position', [10, 40, 80, 22], ...
'String', 'radio 2', ...
'Value', 0);
...
guidata(handles.FigureH, handles);
And the callback:
function myRadio(RadioH, EventData)
handles = guidata(RadioH);
otherRadio = handles.radio(handles.radio ~= RadioH);
set(otherRadio, 'Value', 0);
Michael Adelman
Michael Adelman 2012년 7월 10일

0 개 추천

Thank you, very helpful.
Michael Adelman
Michael Adelman 2012년 7월 10일
편집: Michael Adelman 2012년 7월 10일

0 개 추천

I created uibuttongroup following your suggestions as follows:
% Create the button group. h = uibuttongroup('visible','off', 'Units', 'pixels','Position',[100 717 301 151]); handles.h = h; % Create three radio buttons in the button group. u0 = uicontrol('Style','Radio','String','Analog_Input',... 'pos',[19 75 137 52],'parent',h,'HandleVisibility','off'); u1 = uicontrol('Style','Radio','String','Function_input',... 'pos',[19 31 131 57],'parent',h,'HandleVisibility','off');
% Initialize some button group properties.
set(h,'SelectedObject',[]); % No selection set(h,'Visible','on');
I don't have any Callback. And it doesn't work - any suggestion where the problem might be?
by saying doesn't work, I get multiple selection.

댓글 수: 3

Honglei Chen
Honglei Chen 2012년 7월 10일
The following code worked well for me, I cannot get multiple selection.
h = uibuttongroup('Units', 'pixels');
% Create three radio buttons in the button group.
u0 = uicontrol('Style','Radio','String','Analog_Input',...
'pos',[19 75 137 52],'parent',h,'HandleVisibility','off');
u1 = uicontrol('Style','Radio','String','Function_input',...
'pos',[19 31 131 57],'parent',h,'HandleVisibility','off');
Michael Adelman
Michael Adelman 2012년 7월 11일
thank you.
Jan
Jan 2012년 7월 11일
@Micheal: Please read the instructions for formatting code in this forum. E.g. the "About Matlab Answers" would be a good point to start from.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

태그

질문:

2012년 7월 10일

댓글:

2015년 4월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by