Select multiple radiobutton at the same time
9 ビュー (過去 30 日間)
表示 古いコメント
I have a matlab gui that when it opens has 9 radiobutton i use this to be able to select only one at a time :
switch get(gcbo,'Tag')
case {'Norme0','Norme1022'}
set(gcbo,'Value',1)
case 'NormeEm'
set(gcbo,'Value',1),enable(hno(3))
case 'NormeG53'
set(gcbo,'Value',1),enable(hno(4))
case 'NormeCh'
set(gcbo,'Value',1),enable(hno(5))
case 'NormeUsrV'
set(gcbo,'Value',1),enable(hno(6))
case 'NormeUsrI'
set(gcbo,'Value',1),enable(hno(7))
case 'Norme50160'
set(gcbo,'Value',1),enable(hno(8))
case {'Norme519','Param519-1'}
set(hrb(9),'Value',1)
end
for each case we can select one radiobutton and if we select another it will deselect the one that was selected.
Now i would like if one is selected to be able to select another one if i am on a specific radiobutton
switch get(gcbo,'Tag')
case {'Norme0','Norme1022'}
set(gcbo,'Value',1)
case 'NormeEm'
set(gcbo,'Value',1),enable(hno(3))
if get(gcbo,'Value') ==1
switch get(gcbo,'Tag')
case 'Norme1022'
set(gcbo,'Value',1)
case 'NormeUsrV'
set(gcbo,'Value',1),enable(hno(6))
case 'Norme50160'
set(gcbo,'Value',1),enable(hno(8))
end
end
case 'NormeG53'
set(gcbo,'Value',1),enable(hno(4))
case 'NormeCh'
set(gcbo,'Value',1),enable(hno(5))
case 'NormeUsrV'
set(gcbo,'Value',1),enable(hno(6))
case 'NormeUsrI'
set(gcbo,'Value',1),enable(hno(7))
case 'Norme50160'
set(gcbo,'Value',1),enable(hno(8))
case {'Norme519','Param519-1'}
set(hrb(9),'Value',1),Scr519(hno(9:12))
end
but when launch it it won't work if anyone could help me on this.
採用された回答
Robert U
2022 年 9 月 29 日
編集済み: Robert U
2022 年 9 月 29 日
Hi Ali,
use uipanel() for the checkboxes and uibuttongroup() for the radiobuttons:
myGUI;
function myGUI(~)
gui.fh = figure;
% button group 1 with entry window
gui.btnGrp1.fh = uibuttongroup('Visible','off',...
'Unit','Pixels',...
'Position',[10 10 120 160 ],...
'SelectionChangedFcn',@buttonSelection);
gui.btnGrp1.btn(1).fh = uicontrol(gui.btnGrp1.fh,...
'Style','radiobutton',...
'String','button 1',...
'Visible', 'on',...
'Position',[10 75 100 30]);
gui.btnGrp1.btn(2).fh = uicontrol(gui.btnGrp1.fh,...
'Style','radiobutton',...
'String','button 2',...
'Position',[10 50 100 30]);
gui.btnGrp1.edit.fh = uicontrol(gui.btnGrp1.fh,...
'Style','edit',...
'String','Entry',...
'Position',[10 25 100 30]);
gui.btnGrp1.fh.Visible = 'on';
% button group 2 with entry window
gui.btnGrp2.fh = uibuttongroup('Visible','off',...
'Unit','Pixels',...
'Position',gui.btnGrp1.fh.Position + [ gui.btnGrp1.fh.Position(3)+10 0 0 0],...
'SelectionChangedFcn',@buttonSelection);
gui.btnGrp2.btn(1).fh = uicontrol(gui.btnGrp2.fh,...
'Style','radiobutton',...
'String','button 3',...
'Visible', 'on',...
'Position',[10 75 100 30]);
gui.btnGrp2.btn(2).fh = uicontrol(gui.btnGrp2.fh,...
'Style','radiobutton',...
'String','button 4',...
'Position',[10 50 100 30]);
gui.btnGrp2.edit.fh = uicontrol(gui.btnGrp2.fh,...
'Style','edit',...
'String','Entry',...
'Position',[10 25 100 30]);
gui.btnGrp2.fh.Visible = 'on';
drawnow();
% checkbox group 1 with entry window
gui.chkPanel.fh = uipanel('Unit','Pixels',...
'Position',gui.btnGrp2.fh.Position + [ gui.btnGrp2.fh.Position(3)+10 0 0 0]);
gui.chkBGrp2.btn(1).fh = uicontrol(gui.chkPanel.fh,...
'Style','checkbox',...
'String','checkbox 1',...
'Visible', 'on',...
'Position',[10 75 100 30]);
gui.chkBGrp2.btn(2).fh = uicontrol(gui.chkPanel.fh,...
'Style','checkbox',...
'String','checkbox 2',...
'Position',[10 50 100 30]);
gui.chkBGrp2.edit.fh = uicontrol(gui.chkPanel.fh,...
'Style','edit',...
'String','Entry',...
'Position',[10 25 100 30]);
gui.chkBGrp2.fh.Visible = 'on';
drawnow();
resize();
set(gui.fh,'ResizeFcn',@resize);
function resize(~,~)
% Set all units to normalized
set( findall(gui.fh ,'Units','pixels'),'Units','normalized');
set( findall(gui.fh ,'FontUnits','points'),'FontUnits','normalized');
end
function buttonSelection( ~, event )
disp(['Previous: ' event.OldValue.String]);
disp(['Current: ' event.NewValue.String]);
end
end
Kind regards,
Robert
その他の回答 (1 件)
Walter Roberson
2022 年 9 月 29 日
hf=exemple;
You run the code in exemple and it returns. You are no longer running any code inside exemple but it set up several graphics objects that are sitting around displaying things or waiting for something to happen.
switch get(gcbo,'Tag'); %l'objet ayant provoqué l'appel
gcbo is the current callback object. You are not executing a callback, so gcbo will be empty.
In order to use that switch statement you would need to configure several controls with the same callback function, and the code would need to be inside that callback. (In which case you should probably use the handle automatically passed in as the first parameter of the callback instead of gcbo)
参考
カテゴリ
Find more on Interactive Control and Callbacks 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!