如何获得单选按钮的选定值? [英] how can I get the selected value of radio button?
问题描述
我不是matlab程序员,但是我需要使用matlab创建一个接口! 对于Matlab程序员来说,这个qusetion应该很容易:)
I am not matlab programmer but I need to create an interface using matlab! This qusetion should be very easy for matlab programmers :)
我有一个包含单选按钮组面板"OperationPanel"的界面 ,其中有4个单选按钮,其名称分别为"addBtn,subBtn,divBtn,mulBtn",我有命令按钮,当我单击该按钮以获取所选radioButton的值时,我想要
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
我应该使用什么命令?我在Google上搜索了,发现如果我做
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...
这篇关于如何获得单选按钮的选定值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!