Programmatically setting a radio button from within App Designer

24 ビュー (過去 30 日間)
Peter Cheimets
Peter Cheimets 2025 年 2 月 25 日
回答済み: Image Analyst 2025 年 2 月 26 日
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

回答 (3 件)

Voss
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 件のコメント
Peter Cheimets
Peter Cheimets 2025 年 2 月 25 日
Thank you very much.
Peter
Voss
Voss 2025 年 2 月 25 日
You're welcome! Any questions, let me know. Otherwise, please "Accept" this answer. Thanks!

サインインしてコメントする。


Peter Cheimets
Peter Cheimets 2025 年 2 月 25 日
This is what I ended up doing. I couldn't get the "get" statement to work. So I walked through the lables one by one in the while loop.
Note: state_dic is the dictionary with the app state, and state_dic_keys are the keys from the dictionary extracted so that I could walk through the dictionary in order and it has all of the names in place.
Peter
button_txt = char(state_dic{state_dic_keys(i)});
which_buttons = extractBefore(char(state_dic_keys(i)),'SelectedObject.Text');
buttons = [which_buttons,'Children'];
labels = eval([buttons]);
idx = 1; %initialize the value
while strcmp(labels(idx).Text,button_txt) == 0
idx = idx + 1; %inc idx if this isn't the right button
end
eval([buttons,'(',num2str(idx),').Value = 1;'])
  2 件のコメント
Voss
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);
Peter Cheimets
Peter Cheimets 2025 年 2 月 25 日
This works.
Thanks again.
Peter

サインインしてコメントする。


Image Analyst
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";

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

製品


リリース

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by