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:

回答(2 个)

Voss
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 个评论
Voss
Voss 2025-1-14
Unfortunately I'm not familiar with Simulink.
Walter Roberson
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
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
  1 个评论
Matthew Mishrikey
Matthew Mishrikey 2025-1-14
In my case there will be the same number of selections regardless of category choice.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Modeling 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by