如何获得所选的单选按钮值?

时间:2011-01-07 10:12:42

标签: matlab radio-button

我不是matlab程序员,但我需要使用matlab创建一个界面! 对于matlab程序员来说,这个问题应该很容易:)

我有一个包含单选按钮组面板“OperationPanel”的界面 ,4个radioButtons里面的名字是“addBtn,subBtn,divBtn,mulBtn”,我有命令按钮,我想点击按钮获取所选radioButton的值

我应该使用的是什么?我谷歌,发现如果我做

get(handles.NewValue,'Tag');

我累了,但它不起作用!!我能帮忙吗?

4 个答案:

答案 0 :(得分:12)

这是一个快速示例,用于说明如何获取单选按钮组组件的值:

function simpleGUI
    hFig = figure('Visible','off', 'Menu','none', 'Name','Calculator', 'Resize','off', 'Position',[100 100 350 200]);
    movegui(hFig,'center')          %# Move the GUI to the center of the screen

    hBtnGrp = uibuttongroup('Position',[0 0 0.3 1], 'Units','Normalized');
    uicontrol('Style','Radio', 'Parent',hBtnGrp, 'HandleVisibility','off', 'Position',[15 150 70 30], 'String','Add', 'Tag','+')
    uicontrol('Style','Radio', 'Parent',hBtnGrp, 'HandleVisibility','off', 'Position',[15 120 70 30], 'String','Subtract', 'Tag','-')
    uicontrol('Style','Radio', 'Parent',hBtnGrp, 'HandleVisibility','off', 'Position',[15  90 70 30], 'String','Multiply', 'Tag','*')
    uicontrol('Style','Radio', 'Parent',hBtnGrp, 'HandleVisibility','off', 'Position',[15  60 70 30], 'String','Divide', 'Tag','/')

    uicontrol('Style','pushbutton', 'String','Compute', 'Position',[200 50 60 25], 'Callback',{@button_callback})

    hEdit1 = uicontrol('Style','edit', 'Position',[150 150 60 20], 'String','10');
    hEdit2 = uicontrol('Style','edit', 'Position',[250 150 60 20], 'String','20');
    hEdit3 = uicontrol('Style','edit', 'Position',[200  80 60 20], 'String','');

    set(hFig, 'Visible','on')        %# Make the GUI visible

    %# callback function
    function button_callback(src,ev)
        v1 = str2double(get(hEdit1, 'String'));
        v2 = str2double(get(hEdit2, 'String'));
        switch get(get(hBtnGrp,'SelectedObject'),'Tag')
            case '+',  res = v1 + v2;
            case '-',  res = v1 - v2;
            case '*',  res = v1 * v2;
            case '/',  res = v1 / v2;
            otherwise, res = '';
        end
        set(hEdit3, 'String',res)
    end
end

screenshot

显然,您可以在输入数字上添加更多验证等等......

答案 1 :(得分:2)

您是否已将handles设为hOjbect?另外,在处理单选按钮事件后不要忘记更新句柄。你看过这个Matlab GUI Tutorial了吗?向下滚动到第3部分第5步,查看以下三个单选按钮的示例代码:

function fontSelect_buttongroup_SelectionChangeFcn(hObject, eventdata)

%retrieve GUI data, i.e. the handles structure
handles = guidata(hObject); 

switch get(eventdata.NewValue,'Tag')   % Get Tag of selected object
    case 'fontsize08_radiobutton'
      %execute this code when fontsize08_radiobutton is selected
      set(handles.display_staticText,'FontSize',8);

    case 'fontsize12_radiobutton'
      %execute this code when fontsize12_radiobutton is selected
      set(handles.display_staticText,'FontSize',12);

    case 'fontsize16_radiobutton'
      %execute this code when fontsize16_radiobutton is selected  
      set(handles.display_staticText,'FontSize',16);
    otherwise
       % Code for when there is no match.

end
%updates the handles structure
guidata(hObject, handles);

答案 2 :(得分:2)

如果您使用以下语法,则会收到错误消息:

get(handles.NewValue,'Tag')

你应该使用的是

get(eventdata.NewValue, 'Tag')

重点在于您在查看SelectionChangeFcn时 - 您实际上要查找的是触发的新事件是什么以及与该事件关联的新值是什么。您不必使用“标记” - 您甚至可以使用“字符串”或其他适合您的上下文的属性。

答案 3 :(得分:0)

上面的代码可以投射..

function pushbutton_startProcess_Callback(hObject, eventdata, handles)

    set(handles.edit1,'String',get(handles.edit2,'String'));

            switch get(get(handles.uipanel3,'SelectedObject'),'Tag')
                case 'wavelet_method',  set(handles.edit1,'String','wavelet_method');
                case 'glcm_method',  set(handles.edit1,'String','glcm_method');
                case 'ewd_method',  set(handles.edit1,'String','ewd_method');
                case 'wavelet_gclm_method',       set(handles.edit1,'String','wavelet_glcm_method');
                otherwise, set(handles.edit1,'String','boş');
            end