Can I force a radio button group to initialize with a certain button selected?

As the question says, I want a particular button selected every time the radio button group is initialized. Can I "set()" this somehow?

 採用された回答

Matt Fig
Matt Fig 2012 年 8 月 14 日
編集済み: Matt Fig 2012 年 8 月 14 日
Have a look at the selectedobject property.
h = uibuttongroup('Pos',[0 0 .2 1]);
u0 = uicontrol(h,'Style','Radio','String','Option 1',...
'pos',[10 350 100 30]);
u1 = uicontrol(h,'Style','Radio','String','Option 2',...
'pos',[10 250 100 30]);
u2 = uicontrol(h,'Style','Radio','String','Option 3',...
'pos',[10 150 100 30]);
set(h,'SelectedObject',u2); % Set the object.
.
.
.
If you are using GUIDE, simply put this code at the end of the GUI OpeningFcn. For example, if you GUI is called mygui, then look for mygui_OpeningFcn.
% Use the correct names, for the objects, of course...
set(handles.uipanel3,'selectedobject',handles.radiobutton3)

その他の回答 (2 件)

Justin
Justin 2012 年 8 月 14 日
First alternative is to use GUIDE. This will allow you to set a specific radio button to "selected" everytime the GUI initializes. To do this, open the "Property Inspector" for the radio button and change its "Value" to 1.
Second alternative is to do it programatically. In your code, wherever you initialize the button-group, execute the following line:
>> set(handles.radioTag,'Value','max');
Where, "radioTag" is whatever tag you assigned to your desired radio button when making the gui.

7 件のコメント

Caleb
Caleb 2012 年 8 月 14 日
I'm building the GUI in GUIDE, so this is what I'm looking for. The "Value" works, except for when I recall the button group after I had previously changed the button selection. Do you know how I could get around that?
Justin
Justin 2012 年 8 月 14 日
Can you clarify for me?
It is my understanding that you want the GUI to initialize with a specific radio-button selected (this can be accomplished using GUIDE). At some point, you select a different radio-button by clicking on it. Then, at a later point, you want to change the radio-buttons back to their default value.
Is this correct?
Caleb
Caleb 2012 年 8 月 14 日
Pretty much. The radiobutton group can be called at different times in the process, so each time it's called, I want it to give the same "value".
Sean de Wolski
Sean de Wolski 2012 年 8 月 14 日
huh? Do you want to change its SelectionChangeFcn?
Caleb
Caleb 2012 年 8 月 14 日
Whatever Matt Fig suggested did exactly what I was looking for.
Sean de Wolski
Sean de Wolski 2012 年 8 月 14 日
Matt is known for his telepathic responses :)
Justin
Justin 2012 年 8 月 14 日
編集済み: Justin 2012 年 8 月 14 日
Here's where I ask for a sample of your code...
But, without seeing a sample, I would suggest you simply enter the line of code I wrote above immediately after the code that "calls" your radio-button group.
EDIT: Use the following line. >> set(handles.radioTag,'Value',1);
Matlab button groups are smart enough to change the other buttons to 0 when a single button is set to 1.

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

Sean de Wolski
Sean de Wolski 2012 年 8 月 14 日
Of course.
ug = uibuttongroup;
h(1) = uicontrol('style','radio','parent',ug,'position',[50 50 50 10],'value',true,'string','I''m on');
h(2) = uicontrol('style','radio','parent',ug,'position',[20 50 10 10],'value',false);

カテゴリ

ヘルプ センター および File ExchangeInteractive Control and Callbacks についてさらに検索

質問済み:

2012 年 8 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by