Programmatically setting a radio button from within App Designer
22 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(3 个)
Voss
2025-2-25
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 个评论
Voss
2025-2-25
You're welcome! Any questions, let me know. Otherwise, please "Accept" this answer. Thanks!
Peter Cheimets
2025-2-25
2 个评论
Voss
2025-2-25
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
2025-2-26
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!