MATLAB: Is the selected boundary to zoom in on a figure re-scaled to the axes ratio of the current figure

MATLAB

Using the ‘Zoom In’ option to zoom at a specified boundary of a figure. The zoomed part will be re-scaled to the ratio of the current figure axes.
Does a MATLAB command exists which can suppress the zoom ratio, so that the figure will be re-scaled to the zoomed part of the figure?

Best Answer

A MATLAB command does not exist to suppress the zoomed boundary and re-scale the figure axes.
A possible workaround is the following:
Save the following MATLAB function to 'zoomAspect.m'
function zoomAspect
k = waitforbuttonpress;
point1 = get(gca,'CurrentPoint'); % button down detected
finalRect = rbbox; % return figure units
point2 = get(gca,'CurrentPoint'); % button up detected
point1 = point1(1,1:2); % extract x and y
point2 = point2(1,1:2);
p1 = min(point1,point2); % calculate locations
p2 = max(point1,point2)
P = [p1; p2]
set(gca,'XLim',P(:,1),'YLim',P(:,2),'DataAspectRatio',[1 1 1])
Try the following MATLAB code which creates a figure with a push button. Press the push botton and zoom in on a reactangular part, the figure will be re-scaled.
x = 0:0.1:10;
y = x.^2;
plot(x,y);
set(gca,'DataAspectRatio',[1 1 1],'PlotBoxAspectRatio',[1 1 1]);
uicontrol('style','pushbutton','Callback','zoomAspect','String','Zoom');