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:

Réponses (2)

Voss
Voss le 14 Jan 2025
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 commentaires

Walter Roberson
Walter Roberson le 14 Jan 2025
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
Matthew Mishrikey le 14 Jan 2025
Apologies, I wasn't explicit in my post (though I did tag the question appropriately I think).
I'm trying to do this in Simulink, and I think the radio buttons there are different than the ui groups you would put in a Figure.
If I look at the properties in Simulink, I don't think there is a change callback -
Voss
Voss le 14 Jan 2025
Unfortunately I'm not familiar with Simulink.
Walter Roberson
Walter Roberson le 14 Jan 2025
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.

Connectez-vous pour commenter.

Dan Dolan
Dan Dolan le 14 Jan 2025

0 votes

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 commentaire

Matthew Mishrikey
Matthew Mishrikey le 14 Jan 2025
In my case there will be the same number of selections regardless of category choice.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Modeling dans Centre d'aide et File Exchange

Produits

Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by