Changing axes limits in a while loop

16 views (last 30 days)
Hello everyone,
I've got some code set up to quickly plot some data in 3 subplots and let users cycle through the dataset. I'm trying to implement a dialog box to let users change the zoom on the x-axis, but after setting the limits, the plot springs back to the original axes. Here's my code right now:
figure(figh)
axL = subplot(3,1,1);
axR = subplot(3,1,2);
axRast = subplot(3,1,3);
linkaxes([axL,axR,axRast],'x');
set(axL,'XLimMode','Manual');
j=1;
while j
% SNIP %
plot(axL,x1,y1,'k')
plot(axR,x2,y2,'k')
% SNIP %
plot(axRast,X,Y,ploco(ico));
% SNIP %
response1 = getkey;
j_old=j;
nstims = ds.nrec;
switch response1
case 28 %left arrow, go back
j=j-1;
if j<1;
beep;
j=1;
end
set(axL,'xlim',[starttime endtime])
case 29 %right arrow, advance
j=j+1;
if j>nstims;beep; j=nstims;end
set(axL,'xlim',[starttime endtime])
case 13 %return, also advance
j=j+1;
if j>nstims;beep;j=nstims;end
set(axL,'xlim',[starttime endtime])
case 117 % press U to unzoom
xLims2 = [0,100*ceil(time(end)/100)];
set(axL,'xlim',xLims2);
pan off
case 122 % press Z to zoom; prompt for the X limits and change them.
prompt = {'Enter start time:','Enter end time name:'};
dlg_title = 'Zoom In';
num_lines = 1;
def = {num2str(starttime),num2str(endtime)};
answer = inputdlg(prompt,dlg_title,num_lines,def);
set(axL,'xlim',[str2num(answer{1}) str2num(answer{2})],'XLimMode','Manual')
drawnow;
pan on
case 113 %q means quit and close window
j=0;
close all force;
end
end
It's probably something very silly I'm missing (it's not anything in the SNIPped comments; those are mostly nodes and old code I dont' want to throw out). Is there something I'm not setting right with regards to the axes? I'm completely clueless as to how to proceed. Thanks.

Accepted Answer

Geoff Hayes
Geoff Hayes on 20 Nov 2014
David - it is my understanding that whenever the plot function is invoked, then it resets certain properties of the axes that it is being displayed upon, including the axes limits. Your observed behaviour can be reproduced with the following code
x = -2*pi:0.0001:2*pi;
y1 = sin(x);
y2 = cos(x);
figure;
plot(gca,x,y1);
set(gca,'xlim',[-2 2],'XLimMode','Manual');
plot(gca,x,y2); % and the original axes limits are restored!
One way to avoid this is to use the hold on command which will maintain the axes limits, but unfortunately will keep data that was previously plotted.
An alternative to invoking plot for each of your three axes at each iteration of the while loop, is to just call plot the once (for each axes) and then reset the x and y data at each iteration. There are two benefits to this - you are not calling plot and so the axes limits are maintained, and you are not creating three new graphics handles with each plot made.
From the example sine and cosine example, we can do the following
x = -2*pi:0.0001:2*pi;
y1 = sin(x);
y2 = cos(x);
figure;
h = plot(gca,NaN,NaN,'b'); % nothing is plotted but we now have a handle
set(h,'XData',x,'YData',y1);
set(gca,'xlim',[-2 2],'XLimMode','Manual');
set(h,'XData',x,'YData',y2); % and the current axes limits are maintained!
In your case, outside of your while loop, you would do
hPlotL = plot(axL,NaN,NaN,'k');
hPlotR = plot(axR,NaN,NaN,'k');
hPlotRast = plot(axRast,NaN,NaN,ploco(ico)); % may need to compensate for ploco(ico)?
And then within the while loop do
set(hPlotL,'XData',x1,'YData',y1);
set(hPlotR,'XData',x2,'YData',y2);
set(hPlotRast,'XData',X,'YData',Y);
Try the above and see what happens!

More Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!