how can I get the selected value of radio button?
Asked Answered
U

4

5

I am not matlab programmer but I need to create an interface using matlab! This qusetion should be very easy for matlab programmers :)

I have an interface which contains radio button group panel "OperationPanel" ,4 radioButtons inside it which names are "addBtn, subBtn, divBtn, mulBtn" and I have command button, I want when I click over the button to get the value of the selected radioButton

What is the commad I should use ? I google it and found that if I make

get(handles.NewValue,'Tag');

I tired it but it doesn't work!! Can I hava some help!

Unorganized answered 7/1, 2011 at 10:12 Comment(0)
V
12

Here's a quick example to illustrate how to get the value of a radio-button group component:

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

Obviously you could add more validations on the input numbers and so on...

Vizzone answered 7/1, 2011 at 11:29 Comment(4)
great answer but I am not familiar with GUI programming in matlab, I just make drag and drop to the tools, I add the radioButton Group panel and add all rbuttons indide , but I didn't know how to make them all belongs to the same group.Unorganized
@Alaa: When you create the radiobuttons inside GUIDE, make sure to drop them inside the panelVizzone
I did, and it works now, but I make the action on the radio buttons not on the push button as @gary comtois shows me, I try to call the action method of the radio buttons inside the callback function of the push button, but it doesn't work, do u have any suggestion ?Unorganized
thats exactly what I've shown in the example above. Once the button is pressed, it retrieves the current selection from the radiobuttons group, and apply that operation using a switch block (get(handleRadioGroup,'SelectedObject') then retrieve either the String or Tag property)Vizzone
S
2

Have you set handles to the hOjbect? Also don't forget to update the handle after processing the radio button event. Have you looked at this Matlab GUI Tutorial? Scroll down to Part 3 step 5 to see the following example code for three radio buttons:

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);
Shechem answered 7/1, 2011 at 11:20 Comment(1)
@gray comtois, yes I looked at the Matlab GUI tutorials , but this fucncion "fontSelect_buttongroup_SelectionChangeFcn()" is the action of the buttongroup and I want to make the action on the command button.Unorganized
V
2

If you use this syntax below you will get an error:

get(handles.NewValue,'Tag')

What you should be using is

get(eventdata.NewValue, 'Tag')

The point is the when you are looking at the SelectionChangeFcn - what you are essentially looking for is what is the new event that has fired and what is the new value associated with that event. You don't have to use 'Tag' - you can even use 'String' or other properties that may be appropriate in your context.

Ventris answered 27/3, 2012 at 6:10 Comment(0)
W
0

the code above works on may project ..

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
Wilburwilburn answered 4/7, 2013 at 11:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.