Reputation: 706
I have two radio buttons radiobutton1
,radiobutton2
(not in a group) in GUI, each of them plot on the axes1
a 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?
Upvotes: 0
Views: 1615
Reputation: 10676
I recommend to plot both functions and save their handles. Then toggle with the radio buttons the visibility of the lines. Try out my example:
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
I initialize everything to visible, but it can be done otherwise:
Upvotes: 2