Dynamic dashboard radio button options
이전 댓글 표시
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:


답변 (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일
Instead of deleting and recreating the second radiobutton, you could just change the Text properties of the uiradiobutton()s (if they have the same number of options.)
Matthew Mishrikey
2025년 1월 14일
Voss
2025년 1월 14일
Unfortunately I'm not familiar with Simulink.
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일
0 개 추천
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
카테고리
도움말 센터 및 File Exchange에서 Modeling에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
