how to write a code for checkbox

5 次查看(过去 30 天)
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');

采纳的回答

ChristianW
ChristianW 2013-2-7
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.
  2 个评论
rafi abdul
rafi abdul 2013-2-8
thank you very much this is useful for me.thank you
rafi abdul
rafi abdul 2013-2-24
hi christian,i need one more help for slider.can you please help ,i have raised one question for slider please help

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2013-2-7
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.
  2 个评论
rafi abdul
rafi abdul 2013-2-8
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 2013-2-8
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: http://blogs.mathworks.com/videos/category/gui-or-guide/

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by