how to move the plots when slider is dragged.

4 views (last 30 days)
Hi i have 3 plots in same figure,i want to add slider so that when i drag slider plots should be moved.please help.here is my code
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');

Answers (1)

ChristianW
ChristianW on 25 Feb 2013
Heres a basic example, changing xlim via slider callback.
function slider_example
x = 0:pi/32:8*pi;
y = cos(x).^5;
range = 2; % visible x range
P = [x(1)+range/2 x(end)-range/2]; % min and max mean(xlim Positions)
plot(x,y); axis tight; xlim([x(1) range])
uicontrol('style','slider','units','normalized','position',[0 0 1 0.05],...
'callback',@cb_fcn)
function cb_fcn(hObj,event)
% called when slider was used
val = get(hObj,'value');
mxl = val*(P(2)-P(1))+P(1); % mean xlim, denormalize
xlim([mxl-range/2 mxl+range/2])
end
end

Community Treasure Hunt

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

Start Hunting!