2013-05-25 174 views
0

我在GUI中有两个单选按钮radiobutton1,radiobutton2(不在一个组中),它们每个都在axes1上绘制了一个特定的函数。如果我选择radiobutton1radiobutton2,那么这两个函数将绘制在axes1上。如果我取消选择radiobutton1axes1上将只有radiobutton2的功能,radiobutton1的功能将不再显示。对于radiobutton2也是如此。或者如果我取消选择两个单选按钮,则不会绘制任何图标。Matlab GUI,单选按钮,绘制

我已经定义if循环为每个无线电按钮,诸如

v = get(hObject,'Value'); 

if (v == 1) 
    axes(handles.axes1); 
    plot(sin(x)); 
    hold on; 
else 
    cla; 
end 

我试图cla清除axes1,但它清除所有情节当一个单选按钮是选中。

为了简单起见,我写了两个单选按钮。但我有很多。所以请考虑许多单选按钮的解决方案。

这怎么办?

回答

2

我建议绘制两个函数并保存它们的手柄。然后切换线条的单选按钮可见性。尝试一下我的例子:

function myGUI 

% Plot two functions immediately and save handles 
x = 1:.1:10; 
h.l = plot(x,rem(x,2)-1,'r',x,sin(x),'b'); 

% Create two radio buttons which share the same '@toggle' callback and index 
% the respective line with the position stored in 'UserData' 
h.rb = uicontrol('style','radio','string','redline',... 
       'units','norm','pos',[0.13 0.93 0.1 0.05],... 
       'Value',1, 'callback',@toggle, 'UserData',1); 

h.rb = uicontrol('style','radio','string','blueline',... 
       'units','norm','pos',[0.35 0.93 0.1 0.05],... 
       'Value',1,'callback',@toggle,'UserData',2); 

h.states = {'off','on'}; 

    % Toggle visibility 
    function toggle(src,event) 
     idxLine = get(src,'UserData'); 
     idxState = get(src,'Value')+1; 
     set(h.l(idxLine),'Visible', h.states{idxState})   
    end 
end 

我初始化一切可见的,但它可以以其他方式来实现:

enter image description here