Programmatically setting a radio button from within App Designer
23 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I am trying to change the selected radio button with in a button group from within the app program.
I have an app for which I take the state of the app, put it into a dictionary and save that. Now I want to retrieve the dictionary, and then apply the recovered state definition to the app. This includes selecting the correct radio buttons.
I have the text associated with the selected button, but I can't figure out how to use that to change the selected button to the desired one. If I use a statement like the following: 'app.TelescopeTypeButtonGroup.SelectedObject.Text = Cassegrain ;' then is changes the text assocated with the selected button, but does not change the button that is selected.
I tried to do a series of commands that set the selected button value to "0", hoping that would cycle through all the buttons, but it didn't always cycle through all of the buttons. I have run out of work arounds. Any thoughts?
Thanks
Peter
0 comentarios
Respuestas (3)
Voss
el 25 de Feb. de 2025
You need to refer to the radio button object itself, e.g., using its name in the app structure:
app.TelescopeTypeButtonGroup.SelectedObject = app.Button5;
where app.Button5 refers to the appropriate radio button object in the button group.
Another, less direct, way is, given the Text of some radio button, get the appropriate radio button object from that and then set that to be the SelectedObject, e.g.:
button_txt = 'Cassegrain';
buttons = app.TelescopeTypeButtonGroup.Children;
labels = get(buttons,{'Text'});
idx = strcmp(labels,button_txt);
app.TelescopeTypeButtonGroup.SelectedObject = buttons(idx);
The attached app shows both ways in its startupFcn.
2 comentarios
Voss
el 25 de Feb. de 2025
You're welcome! Any questions, let me know. Otherwise, please "Accept" this answer. Thanks!
Peter Cheimets
el 25 de Feb. de 2025
2 comentarios
Voss
el 25 de Feb. de 2025
which_buttons = extractBefore(char(state_dic_keys(i)),'SelectedObject.Text');
I assume which_buttons produced there is a character vector that's something like 'app.TelescopeTypeButtonGroup.', i.e., the name of the radio button group with a period on the end.
If that's true, then the code can be:
button_txt = char(state_dic{state_dic_keys(i)});
buttongroup = eval(extractBefore(char(state_dic_keys(i)),'.SelectedObject.Text'));
buttons = buttongroup.Children;
labels = get(buttons,{'Text'});
idx = strcmp(labels,button_txt);
buttongroup.SelectedObject = buttons(idx);
Image Analyst
el 26 de Feb. de 2025
I wouldn't use eval. What if you just set the other buttons also if setting the one you want doesn't change the others automatically, like
% Set radio button you want
app.radioButton1.Value = "on";
% Deselect others
app.radioButton2.Value = "off";
app.radioButton3.Value = "off";
0 comentarios
Ver también
Categorías
Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!