how can I get the selected value of radio button?
I am not matlab programmer but I need to create an interface using matlab! This qusetion should be v开发者_运维技巧ery 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!
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
Obviously you could add more validations on the input numbers and so on...
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);
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.
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
精彩评论