Dynamic dashboard radio button options
16 次查看(过去 30 天)
显示 更早的评论
Hello!
I was curious if it was possible to make the options of a radio button change based on another radio button.
This is a manually created example that shows visually what I want, but doesn't actually work. If you pick a different category, the selection options automatically change:


0 个评论
回答(2 个)
Voss
2025-1-14
Yes, you can have the SelectionChangedFcn of the first ('Category') button group delete and re-create the radiobuttons of the second ('Selection') button group.
Here's an example, creating all components programmatically in a figure. It would work the same in a uifigure or even in App Designer (i.e., a uifigure with auto-generated code that creates the components). The important aspect is that the code in cb_group1 deletes and creates the radiobuttons in uibuttongroup group2.
function main_gui()
f = figure();
group1 = uibuttongroup(f, ...
'Title','Category', ...
'Units','pixels', ...
'Position',[50 100 120 110], ...
'SelectionChangedFcn',@cb_group1);
rb1 = [ ...
uicontrol(group1, ...
'Style','radiobutton', ...
'String','ones', ...
'Units','pixels', ...
'Position',[10 70 100 18]) ...
uicontrol(group1, ...
'Style','radiobutton', ...
'String','tens', ...
'Units','pixels', ...
'Position',[10 40 100 18]) ...
uicontrol(group1, ...
'Style','radiobutton', ...
'String','hundreds', ...
'Units','pixels', ...
'Position',[10 10 100 18]) ...
];
group2 = uibuttongroup(f, ...
'Title','Selection', ...
'Units','pixels', ...
'Position',[220 80 120 140], ...
'SelectionChangedFcn',@cb_group2);
rb2 = [];
cb_group1(group1)
function cb_group1(src,~)
disp(src.SelectedObject.String)
idx = find(rb1 == src.SelectedObject,1);
N = 4;
vals = string((1:N).*10.^(idx-1));
delete(rb2);
rb2 = arrayfun(@(ii)uicontrol(group2, ...
'Style','radiobutton', ...
'String',vals(ii), ...
'Units','pixels', ...
'Position',[10 10+30*(N-ii) 100 18]),1:N);
end
function cb_group2(src,~)
disp(src.SelectedObject.String)
end
end
4 个评论
Walter Roberson
2025-1-14
I would tend to suspect that if you were to do something like use a MATLAB Function block to set_param (or something similar) that the buttons label could be changed. The question would be whether the updates to the button text would happen "immediately" or if instead they would be displayed until the next time the simulation stopped or paused.
Dan Dolan
2025-1-14
Matthew,
What you are asking is absolutely possible, though the practicality depends on whether or not the number of options changes between categories. For the example you show, it's simply a matter of setting the SelectionChangedFcn callback for the button group to swap out Selection items.
The problem comes if the "ones" category has more/less selections than "tens" or "hundreds" do. If so, you either have to add/remove radio buttons on the fly or make items visible/invisible as needed. It might be easier to implement what you want with a set of uidropdown objects, or possibly a set of uilistboxes. These more naturally allow the number of items to change without resizing the graphic.
Dan
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
