0

I have two radio buttons radiobutton1,radiobutton2 (not in a group) in GUI, each of them plot on the axes1a specific function. If I select radiobutton1 and radiobutton2 the two functions will be plotted on the axes1. If I unselect radiobutton1 there will be only function of radiobutton2 on the axes1, the function of radiobutton1 will not be seen anymore. The same for radiobutton2. Or If I unselect two radio buttons nothing will be plotted.

I have defined if loop for each radio buttons such as

    v = get(hObject,'Value');

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

I tried cla to clear axes1, but it clears all the plots when one radio button is unselected.

I have written two radio buttons for the sake of simplicity. But I have many of them. So please think the solution for many radio buttons.

How can this be done?

4

1 回答 1

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

我将所有内容初始化为可见,但可以通过其他方式完成:

在此处输入图像描述

于 2013-05-25T16:02:24.610 回答