enable/disable menu next to radiobuttons when those are selected/deselected

Hi,
I have a gui which takes 7 parameter which are associated with 2 radiobutton groups. What i have managed to do is read the parameters value and enable the radiobuttons and there associated popmenus that are linked to these parameters.
Here is my gui here for exemple i have the parameters Indx which value is 4 associated with the radiobutton G5/3 so this radiobutton takes the value 1 and with it i have the parameter ParamStr which value is 1 which enable the popmenu linked to radiobutton and select the first element of the popmenu. Then i have the parameter Indx2 which value is 3 associated with the radiobutton EN50160 and same i have a parameter ParamStr5 of value 2 which enable the popmenu and takes it's second value.
Here is my program :
function kgexec4n(Init,action)
%KGEXEC4N boite de dialogue "norme" associée à kgexec4
%CAVIAR2, © ALSTOM + OL 2000/04-2003/03
error(nargchk(0,1,nargin));
persistent blkParamNorm
%%%% ouverture boite
if nargin==1
%"full block path name" du bloc ParamNorme
blkParamNorm=gcb;
if ~strcmp(get_param(blkParamNorm,'MaskType'),'ParamNor')
errordlg(kverranorm(14),'Caviar3','modal')
return
end
%création figure
hf=kgui4n;
%en cas de librairie verrouillée on interdit OK
%x=get_param(get_param(gcs,'Parent'),'Lock'); %modif2.4 10/03/2006
rep=findstr(gcs,'/');
if isempty(rep)
active_sys=gcs;
else
active_sys=gcs(1:rep-1)
end
x=get_param(active_sys,'Lock')
set( findobj(hf,'Tag','NormeOK'), 'Enable', onoff(~onoff(x)) )
else
hf=gcbf;
end
%%%% handle des contrôles et dévalidation de tout
hrb=[findobj(hf,'Tag','Norme0')
findobj(hf,'Tag','NormeEm')
findobj(hf,'Tag','NormeG53')
findobj(hf,'Tag','NormeCh')
findobj(hf,'Tag','NormeUsrI')
findobj(hf,'Tag','Norme519')];%First radiobutton group
hrbVal=get(hrb,'Value');
set(hrb,'Value',0)
hrt=[findobj(hf,'Tag','Norme1022')
findobj(hf,'Tag','NormeUsrV')
findobj(hf,'Tag','Norme50160')];%Second radiobutton group
hrtVal=get(hrt,'Value');
set(hrt,'Value',0)
hno=[findobj(hf,'Tag','Param0')
findobj(hf,'Tag','Param1022')
findobj(hf,'Tag','ParamEm')
findobj(hf,'Tag','ParamG53')
findobj(hf,'Tag','ParamCh')
findobj(hf,'Tag','ParamUsrV')
findobj(hf,'Tag','ParamUsrI')
findobj(hf,'Tag','Param50160')
findobj(hf,'Tag','Param519-1')
findobj(hf,'Tag','Param519-2')
findobj(hf,'Tag','Param519-3')
findobj(hf,'Tag','Param519-4')];%all popmenu and edit box handles
set(hno,'Enable','off')
if nargin==1
try
no=str2num(get_param(blkParamNorm,'Indx'));%look the parameter Indx in the block which tells what radiobutton of the first group need to be selected
set(hrb(no),'Value',1) %Select the radiobutton(one of the first group) (no=2 select Emeraude, no=3 select G5/3 ....)
switch no
case 2%Emeraude
x=get_param(blkParamNorm,'ParamStr');
set(hno(3),'String',x,'Enable','on')% Enable the popmenu or edit box associated with the radiobutton emeraude
case 5%UsrI
x=get_param(blkParamNorm,'ParamStr');
set(hno(7),'String',x,'Enable','on')
case 3%G5/3
x=get_param(blkParamNorm,'ParamStr');
set(hno(4),'Value',str2num(x),'Enable','on')
case 4%Chine
x=get_param(blkParamNorm,'ParamStr');
set(hno(5),'Value',str2num(x),'Enable','on')
case 6%IEEE519
x=get_param(blkParamNorm,'ParamStr');
set(hno(9),'Value',str2num(x))
x=get_param(blkParamNorm,'ParamStr2');
set(hno(10),'String',x)
x=get_param(blkParamNorm,'ParamStr3');
set(hno(11),'Value',str2num(x))
x=get_param(blkParamNorm,'ParamStr4');
set(hno(12),'Value',str2num(x))
Scr519(hno(9:12))
end
no1=str2num(get_param(blkParamNorm,'Indx2'));%look the parameter Indx in the block which tells what radiobutton of the second group need to be selected
set(hrt(no1),'Value',1)%Select the radiobutton(one of the second group) (no=2 select UsrV, no=3 select EN50160 ....)
switch no1
case 2%UsrV
x1=get_param(blkParamNorm,'ParamStr5');
set(hno(6),'String',x1,'Enable','on')
case 3%EN50160
x1=get_param(blkParamNorm,'ParamStr5');
set(hno(8),'Value',str2num(x1),'Enable','on')
end
catch
error(kverranorm(7))
end%try
return
end
function enable(h)
set(h,'Enable','on')
%%%%%%%%%%%%
function Scr519(h)
enable(h)
x=get(h(3),'Value');%n° ISC/IL en cours
switch get(h(1),'Value')
case {1,2}
if (x<1)||(x>5),x=1;end
ch='Iscr<20|20<Iscr<50|50<Iscr<100|100<Iscr<1k|1000<Iscr';
case 3
if (x<1)||(x>2),x=1;end
ch='Iscr<25|25<Iscr>50|Iscr>=50';
otherwise
error(kverranorm(6))
end
set(h(3),'String',ch,'Value',x)
What i would like to do now is for each of my buttongroups when i select one of the radiobuttons it enables the popmenu or edit box that is beside the radiobutton. I have no idea how to do that as i have two radiobuttongroups and i tried using a switch but it doesn't work with 2 groups but works when i have one :
%%%% gestion dynamique de la boite
switch get(gcbo,'Tag'); %l'objet ayant provoqué l'appel
%dialogue dynamique
case 'Norme0'
set(gcbo,'Value',1)
case 'Norme1022'
set(gcbo,'Value',1)
case 'NormeUsrV'
set(gcbo,'Value',1),enable(hno(6))
case 'Norme50160'
set(gcbo,'Value',1),enable(hno(8))
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 'NormeUsrI'
set(gcbo,'Value',1),enable(hno(7))
case {'Norme519','Param519-1'}
set(hrb(9),'Value',1),Scr519(hno(9:12))
end
If anyone could suggest me some way to make it work for two buttongroups i would help a lot.

 Akzeptierte Antwort

You are hiding how you created the radio buttons in the gui creation, but a big question is whether when you say "2 button groups", do you really mean you created radio buttons within matlab button groups (uibuttongroup), or do you mean you created radio buttons, and some of them belong in your mind to 1 group, and the rest to another?
If you use uifigure and uibuttongroup, below seems to be Matlab's recommended way(do doc uiradiobutton).
But this is somewhat annoying because you can't automatically place children of a buttongroup in a grid layout.
fig = uifigure
uig = uigridlayout(fig,[1,2])
bgA = uibuttongroup(uig,"SelectionChangedFcn",@bgAChangedFcn,"Title","GrpA")
bgB = uibuttongroup(uig,"SelectionChangedFcn",@bgBChangedFcn,"Title","GrpA")
uigA = uigridlayout(bgA,[3,1])
uigB = uigridlayout(bgB,[3,1])
rbA(1) = uiradiobutton(bgA,"Text","GrpA 1","Position",[10,10,100,22])
rbA(2) = uiradiobutton(bgA,"Text","GrpA 2","Position",[10,42,100,22])
rbA(3) = uiradiobutton(bgA,"Text","GrpA 3","Position",[10,74,100,22])
rbB(1) = uiradiobutton(bgB,"Text","GrpB 1","Position",[10,10,100,22])
rbB(2) = uiradiobutton(bgB,"Text","GrpB 2","Position",[10,42,100,22])
rbB(3) = uiradiobutton(bgB,"Text","GrpB 3","Position",[10,74,100,22])
function bgAChangedFcn(src,evnt)
src
evnt
evnt.NewValue
end
function bgBChangedFcn(src,evnt)
src
evnt
evnt.NewValue
end

7 Kommentare

Ali
Ali am 7 Okt. 2022
Bearbeitet: Ali am 7 Okt. 2022
I put the the gui creation file in this comment. You will see in it, that i have set up my radiobutton as you said in matlab button groups (uibuttongroup). Moreover i use matlab 2012 so uigridlayout isn't availble on it the same thing goes for uiradiobutton.
Here is two files with what i tried to do to be able to unlock/lock the menus next to the different radiobuttons but i always get an error with the switch giving me this error :
Error using findobjinternal
Incomplete property-value pair.
Error in C:\Program
Files\MATLAB\R2012a\toolbox\matlab\graphics\private\findobjhelper.p>findobjhelper
(line 31)
Error in findobj (line 45)
h = findobjhelper( varargin{:} );
Error in exemple2 (line 37)
switch get(findobj(hf,'Tag'))
Ah ok, too bad about the version. I hope this example works in 2012
fig = figure('Units','normalized');
uibgA = uibuttongroup(fig,'Position',[0,0.5,0.5,0.5],'Title','ButtonGroupA')
uibgB = uibuttongroup(fig,'Position',[0,0.0,0.5,0.5],'Title','ButtonGroupB')
uipA = uipanel(fig,'Position',[0.5,0.5,0.5,0.5],'Title','Group A')
uipB = uipanel(fig,'Position',[0.5,0.0,0.5,0.5],'Title','Group B')
grpARadioLabels = {
'Choice 1','Tooltip 1'
'Choice 2','Tooltip 2'
'Choice 3','Tooltip 3'
};
grpBRadioLabels = {
'Choice 1','Tooltip 1'
'Choice 2','Tooltip 2'
'Choice 3','Tooltip 3'
'Choice 4','Tooltip 4'
};
for i = 3:-1:1
rbA(i) = uicontrol(uibgA,'Style','radiobutton',...
'String',grpARadioLabels{i,1},...
'TooltipString',grpARadioLabels{i,2},...
'Position',[10,10+i*22,200,22] ...
)
prmsA(i) = uicontrol(uipA,'Style','edit',...
'Position',[10,10+i*22,100,22],...
'Tag',grpARadioLabels{i,1},...
'Enable','off',...
'String',sprintf('param %d',i))
end
for i = 4:-1:1
rbB(i) = uicontrol(uibgB,'Style','radiobutton',...
'String',grpBRadioLabels{i,1},...
'TooltipString',grpBRadioLabels{i,2},...
'Position',[10,10+i*22,200,22] ...
)
prmsB{i} = uicontrol(uipB,'Style','edit',...
'Position',[10,10+i*22,100,22],...
'Tag',grpBRadioLabels{i,1},...
'Enable','off',...
'String',sprintf('param %d',i))
end
i = 3
prmsB{i}(2) = uicontrol(uipB,'Style','popupmenu',...
'Position',[120,10+i*22,100,22],...
'Tag',grpBRadioLabels{i,1},...
'Enable','off',...
'String',{'option A','option D','option C','option D'})
set(uibgA,'SelectionChangedFcn',@(o,e)bgChangeFcn(o,e,prmsA))
set(uibgB,'SelectionChangedFcn',@(o,e)bgChangeFcn(o,e,prmsB))
function bgChangeFcn(src,evnt,prms)
allprms = [prms{:}];
set(allprms,'Enable','off');
lbl = evnt.NewValue.String;
set(findobj(allprms,'Tag',lbl),'Enable','on');
end
Sorry for the late answer and thx for giving an exemple for the 2012 version but i tried to use it but i don't think the function bgChangeFcn works on 2012 as it gives this error code :
Error using uitools.uibuttongroup/set
The name 'SelectionChangedFcn' is not an accessible property for an instance of class 'uibuttongroup'.
Error in exemple1 (line 49)
set(uibgA,'SelectionChangedFcn',@(o,e)bgChangeFcn(o,e,prmsA))
I don't have a way to look at documentation for uibuttongroup for 2012, but you could look at it through your matlab. Look for a property like callbackfcn or something that sounds like it would be a "selectionchangedfcn".
Anyway, the takeaways of my example as I see them are:
  1. let the uibuttongroup handle triggering the enable/disable of the various UIControls, and rely on directly passing (I didn't study your example hard enough to understand how you were achieving the trigger)
  2. Save a few lines of code by creating radio buttons in a loop - it wouldn't generalize easily to the parameters, since you can have more than 1 parameter per radio button, but hopefully the concept is useful
  3. instead of switch/case-ing on tag, it's shorter to find the object whose tag is the same and act on it
Hope that helps
thanks a lot for your suggestion it helped a lot and it managed to work on matlab 2012 it's SelectionchangeFcn.
Hah, present vs past tense? That's funny. Anyway, glad it helped!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2012a

Gefragt:

Ali
am 7 Okt. 2022

Kommentiert:

am 10 Okt. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by