MATLAB: How to write a code for checkbox

addaxischeckboxguide

Hi i have 3 plots in same figure,i want 3 checkboxes each plot and its corresponding axis shold be assigned to individal checkboxes. when i uncheck the checkbox plot and axis corresponding to that checkbox should be disappeared and again when i check the checkbox plot and axis of that checkeckbox should be displayed.here is my code,please help
x = 0:20;
N = numel(x);
y1 = rand(1,N);
y2 = 5.*rand(1,N)+5;
y3 = 50.*rand(1,N)-50;
%# Some initial computations:
axesPosition = [110 40 200 200]; %# Axes position, in pixels
yWidth = 30; %# y axes spacing, in pixels
xLimit = [min(x) max(x)]; %# Range of x values
xOffset = -yWidth*diff(xLimit)/axesPosition(3);
%# Create the figure and axes:
figure('Units','pixels','Position',[200 200 330 260]);
h1 = axes('Units','pixels','Position',axesPosition,...
'Color','w','XColor','k','YColor','r',...
'XLim',xLimit,'YLim',[0 1],'NextPlot','add');
h2 = axes('Units','pixels','Position',axesPosition+yWidth.*[-1 0 1 0],...
'Color','none','XColor','k','YColor','m',...
'XLim',xLimit+[xOffset 0],'YLim',[0 10],...
'XTick',[],'XTickLabel',[],'NextPlot','add');
h3 = axes('Units','pixels','Position',axesPosition+yWidth.*[-2 0 2 0],...
'Color','none','XColor','k','YColor','b',...
'XLim',xLimit+[2*xOffset 0],'YLim',[-50 50],...
'XTick',[],'XTickLabel',[],'NextPlot','add');
xlabel(h1,'time');
ylabel(h3,'values');
%# Plot the data:
plot1=plot(h1,x,y1,'r');
plot2=plot(h2,x,y2,'m');
plot3=plot(h3,x,y3,'b');

Best Answer

Code for the checkboxes uicontrol:
hax = [h1;h2;h3]; % axes handles
hcb = [0;0;0]; % checkbox handle (preallocate)
cb_text = {'r ','m ','b '}; % checkbox text
for i = 1:3
hcb(i) = uicontrol('Style','checkbox','Value',1,...
'Position',[-30+i*40 1 40 15],'String',cb_text{i});
end
set(hcb,'Callback',{@box_value,hcb,hax});
And the callback function:
function box_value(hObj,event,hcb,hax) %#ok<*INUSL
% Called when boxes are used
v = get(hObj,'Value');
I = find(hcb==hObj);
%[axes visibility]:
s = {'off','on'};
ycol_r = {[1 1 1]*0.8,'r'}; % ycolor for red plot axes
if I == 1
set(hax(I),'YColor',ycol_r{v+1})
else
set(hax(I),'Visible',s{v+1})
end
%[line visibility]:
hl = findobj(hax(I),'Type','line'); % line handles
set(hl,'Visible',s{v+1})
The function turns the visibility for axes and plot on and off. Your red axes cant be turn off, because its needed for the other axes. Thats why just the yaxis color is changed to the background color.