Clear Filters
Clear Filters

how to write a code for checkbox

4 views (last 30 days)
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 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,...
'XLim',xLimit,'YLim',[0 1],'NextPlot','add');
h2 = axes('Units','pixels','Position',axesPosition+yWidth.*[-1 0 1 0],...
'XLim',xLimit+[xOffset 0],'YLim',[0 10],...
h3 = axes('Units','pixels','Position',axesPosition+yWidth.*[-2 0 2 0],...
'XLim',xLimit+[2*xOffset 0],'YLim',[-50 50],...
%# Plot the data:

Accepted Answer

ChristianW on 7 Feb 2013
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});
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
%[line visibility]:
hl = findobj(hax(I),'Type','line'); % line handles
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.
rafi abdul
rafi abdul on 8 Feb 2013
thank you very much this is useful for me.thank you
rafi abdul
rafi abdul on 24 Feb 2013
hi christian,i need one more help for slider.can you please help ,i have raised one question for slider please help

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 7 Feb 2013
You're going to have to create checkboxes. Why not use GUIDE? Just place them on the window and right click and go to the callback and put in your code. Otherwise you'll have to do the same thing for checkboxes like you did with the axes and that's tedious.
Do you know that if you turn the axes off or make them invisible, that only affects the two axes (x & y) and the tick marks and labels? The background and stuff plotted or displayed in the axes will still be there - probably not what you want or were expecting. A work about is to put the axes in a panel and then you can turn the panel's visibility on or off and that will hide the entire axes and all its contents.
So, in the callback for the axes you can turn the panels on or off, or maybe you just want to call "cla reset" on the appropriate axes.
rafi abdul
rafi abdul on 8 Feb 2013
for the same requirement how to create multiple axis using GUIDE.offsetting of y-axis where to define this in propertyeditor.can you please help me how to proceed using guide for the same requirement
Image Analyst
Image Analyst on 8 Feb 2013
In GUIDE, click on a control such as a checkbox or an axes in the panel of available controls. Click and drag it out to the size you want on the figure. See Doug Hull's videos for quick and easy basic training on GUIDE:

Sign in to comment.


Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!