Programmatically setting a radio button from within App Designer

조회 수: 22 (최근 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
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 CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by