Matlab GUI,单选按钮,绘图

时间:2013-05-25 15:07:39

标签: matlab radio-button matlab-guide

我在GUI中有两个单选按钮radiobutton1radiobutton2(不在一个组中),每个按钮都在axes1特定函数上绘制。如果我选择radiobutton1radiobutton2,则会在axes1上绘制两个函数。如果我取消选择radiobutton1radiobutton2上只有axes1的功能,则radiobutton1的功能将不再显示。 radiobutton2也是如此。或者如果我取消选择两个单选按钮,则不会绘制任何内容。

我为每个单选按钮定义了if循环,例如

    v = get(hObject,'Value');

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

我尝试cla清除axes1,但是当取消选中一个单选按钮时,它会清除所有图。

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

如何做到这一点?

1 个答案:

答案 0 :(得分: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

相关问题