MATLAB: Return to previous zoom level/location

axis

Let's say I have created a figure using imagesc
imagesc(X(1,:),Y(:,1),I)
and I have then used the manual zoom tool to look at a particular region in the plot. I then do some changes to I (e.g. I2 = imdilate(I,ones(3))) and plot it again, say
imagesc(X(1,:),Y(:,1),I2)
and I want to zoom at the same extent again. What would be best practice to do so?
So far I tried to employ the Xlim and Ylim axis properties, such that
imagesc(X(1,:),Y(:,1),I); % zoom in somewhere manually
b = get(gca,'Xlim');
c = get(gca,'Ylim');
and then
imagesc(X(1,:),Y(:,1),I2)
set(gca,'Xlim',b,'Ylim',c)
but this won't allow me to zoom out afterwards.

Best Answer

Here is an example.
load clown
image(X)% Now zoom in on the eye.
% After zooming in, do this:
.
.
L = get(gca,{'xlim','ylim'}); % Get axes limits.
X = ind2rgb(X,map);
image(X)
zoom reset
set(gca,{'xlim','ylim'},L)
% Now the plot should be zoomed in around the eye again, but you can also zoom out.