Accessing data from Radio Buttons
12 次查看(过去 30 天)
显示 更早的评论
Hello,
perhaps this is a better way to phrase my question. I have a group of radio buttons within a panel. I need to know which one is selected to use in simple "If/Then" statements
If (this radio button is selected && this menu item is clicked) Execute this
I am using the code from an online example
I am trying
get(handles.radiobutton1, 'Value')
to access what it is but no matter which button of the four I have selected all handles are apprearing as zero.
Thank you
0 个评论
采纳的回答
Matt Fig
2011-6-29
I don't know why that would be so. Here is an example. Choosing a radiobutton prints the selected state to the command window.
function [] = radio_panel()
P = uibuttongroup('selectionc',@btngrp_sel,...
'units','pix',...
'pos',[10 10 100 200]);
for ii = 1:4
r(ii) = uicontrol(P,'style','radio',...
'units','pix',...
'pos',[10 10+50*(ii-1) 80 30],...
'string',['radio',num2str(ii)]);
end
guidata(gcf,r) % Save the handle to the radio buttons.
set(P,'selectedO',[]) % Initially no selection.
function [] = btngrp_sel(varargin)
r = guidata(gcbf);
get(r,'value') % Should print a 4 element array. Only one non-zero
0 个评论
更多回答(2 个)
Fangjun Jiang
2011-6-29
Bill, let's work out this "button group" issue first. Your code in previous post is overly complicated.
Start with a new GUI, drag and drop a "button group", drag and drop 4 radio buttons inside the "button group" panel.
In the SelectionChangeFcn callback of the "button group", type in the following single line.
disp(get(hObject,'Tag'));
Then run your GUI and click different buttons, see what happened in the command window. That's how it works.
0 个评论
Walter Roberson
2011-6-29
Get the SelectedObject property of the uibuttongroup, and get() the Tag of that object (having set the tags uniquely when you created the buttons.)
Note: in previous postings, people indicated that one of the buttons must be selected; that is incorrect. You can have nothing selected by setting the SelectedObject property to []
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dialog Boxes 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!