Programmatically generating radio buttons based on pop-up menu selection

1 view (last 30 days)
Hi everyone,
I am making a GUI to help display beach elevation profiles. I have a popup menu that has the option to select which profile line I want to see. Based on what profile I pick, I then want to create x amount of radio buttons in a seperate panel to represent different surveys for that benchmark (certain profiles have more or less surveys to display than others) that I want to toggle on and off. Is there a way programmatically generate the x amount of radio buttons in the panel after selecting the profile desired? Or do the radio buttons need to be created before selection?
Thanks!

Accepted Answer

Tommy
Tommy on 25 Apr 2020
Would something like this work for you?
f = figure;
data.p1 = uipanel(f, 'Position', [0 0.5 1 0.5]);
data.p2 = uipanel(f, 'Position', [0 0 1 0.5]);
data.popup = uicontrol(data.p1,...
'Style', 'popupmenu',...
'String', cellstr(('1':'3')'),...
'Value', 2,...
'Callback', @popupChangedFcn);
data.rbg = uibuttongroup(data.p2,...
'Position', [0.05 0.1 0.1 0.8],...
'SelectionChangedFcn', @rbChangedFcn);
% Initialize to second group of buttons
createRB = @(s, ypos) uicontrol(data.rbg,...
'Style', 'radiobutton',...
'String', s,...
'Units', 'normalized',...
'Position', [0.1 ypos 0.9 0.2]);
createRB('2a', 0.8);
createRB('2b', 0.6);
createRB('2c', 0.4);
createRB('2d', 0.2);
createRB('2e', 0);
% Store data
guidata(f, data);
function popupChangedFcn(src, ~)
data = guidata(src);
value = data.popup.Value;
% Delete all previous radio buttons
children = data.rbg.Children;
for c = 1:numel(children)
delete(children(c));
end
% Add new radio buttons based on value
createRB = @(s, ypos) uicontrol(data.rbg,...
'Style', 'radiobutton',...
'String', s,...
'Units', 'normalized',...
'Position', [0.1 ypos 0.9 0.2]);
switch value
case 1
createRB('1a', 0.8);
createRB('1b', 0.6);
createRB('1c', 0.4);
createRB('1d', 0.2);
createRB('1e', 0);
case 2
createRB('2a', 0.8);
createRB('2b', 0.6);
createRB('2c', 0.4);
createRB('2d', 0.2);
createRB('2e', 0);
case 3
createRB('3a', 0.8);
createRB('3b', 0.6);
createRB('3c', 0.4);
createRB('3d', 0.2);
createRB('3e', 0);
end
end
function rbChangedFcn(src, ~)
data = guidata(src);
disp(data.rbg.SelectedObject.String)
end
You can of course vary the number of buttons created in each case, and you could even update the size of the radio button group to get rid of any blank space.

More Answers (0)

Categories

Find more on App Building 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!