Select multiple radiobutton at the same time

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.

7 comentarios

Jan
Jan el 28 de Sept. de 2022
Please mention, what "it won't work" means.
It is the natur of radio buttons, that only one of a group is selected. If you want to allow multiple selections, use check boxes.
Walter Roberson
Walter Roberson el 28 de Sept. de 2022
If they are in a uibuttongroup then only one can be active, and that is enforced by the container.
Ali
Ali el 28 de Sept. de 2022
ok i will try with checkboxes because when i say it won't work it means that i can still only select one and not two
Walter Roberson
Walter Roberson el 28 de Sept. de 2022
The problem is not radio button, it is the fact that you are controlling them by a button container. The exact same thing happens with checkboxes if you put them in a uibuttongroup
Ali
Ali el 28 de Sept. de 2022
Editada: Ali el 28 de Sept. de 2022
I didn't put them in a uibuttongroup they are just all in the same figure that's all. But even with checkboxes it still doesn't work.
dpb
dpb el 28 de Sept. de 2022
Think it'll take a minimum working example here to figure out just what the gui is for anybody to try to reproduce/debug. Only 2 or 3 is as good as 20...
Here is an exemple we have the gui which is exemple one which posses 3 checkboxes and the other function exemple2 which run the gui and what i want it to do is if UsrV is selected UsrI can't be selected and i want IEEE519 to be checkable at all times so that i select at least two checkboxes at all time. But on this exemple i get the following error
SWITCH expression must be a scalar or string constant.
Error in exemple2 (line 11)
switch get(gcbo,'Tag');

Iniciar sesión para comentar.

 Respuesta aceptada

Robert U
Robert U el 29 de Sept. de 2022
Editada: Robert U el 29 de Sept. de 2022
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

4 comentarios

Ali
Ali el 29 de Sept. de 2022
thank you i will try that
I tried to set up two uipanels so that i can only select one checkbox in each of one but when i put them in place when i launch my programm matlab gives me this code error :
??? Warning: Struct field assignment overwrites a value with class "double".
See MATLAB 7.0.4 Release Notes, Assigning Nonstructure Variables As Structures Displays Warning for details.
> In Caviar\private\kgui4n at 133
In kgexec4n at 17
??? Warning: Struct field assignment overwrites a value with class "double".
See MATLAB 7.0.4 Release Notes, Assigning Nonstructure Variables As Structures Displays Warning for details.
> In Caviar\private\kgui4n at 247
In kgexec4n at 17
ligne 17 of kgexec4n is
hf=kgui4n;
ligne 133 of kgui4n is
h0.c=uipanel('Unit','Pixels')
with h0 being my figure
and ligne 247 of kgui4n is :
ho.t=uipanel('Unit','Pixels')
Robert U
Robert U el 29 de Sept. de 2022
You should not assign the handle of uipanel to the figure handle. I use "gui" as structure array to collect and organize handles of different graphical objects.
Ali
Ali el 29 de Sept. de 2022
ok thx for the answer

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 29 de Sept. de 2022
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)

1 comentario

Ali
Ali el 29 de Sept. de 2022
The problem came from the exemple i gave, in the program i use i have no problems with this line as i have differents checkboxes and pushbutton having the same callback function which is kgexec4n

Iniciar sesión para comentar.

Categorías

Más información sobre Interactive Control and Callbacks en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

Ali
el 28 de Sept. de 2022

Comentada:

Ali
el 29 de Sept. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by