📄 zoom.m
字号:
function out = zoom(varargin)
%ZOOM Zoom in and out on a 2-D plot.
% ZOOM with no arguments toggles the zoom state.
% ZOOM(FACTOR) zooms the current axis by FACTOR.
% Note that this does not affect the zoom state.
% ZOOM ON turns zoom on for the current figure.
% ZOOM XON or ZOOM YON turns zoom on for the x or y axis only.
% ZOOM OFF turns zoom off in the current figure.
% ZOOM RESET resets the zoom out point to the current zoom.
% ZOOM OUT returns the plot to its current zoom out point.
% If ZOOM RESET has not been called this is the original
% non-zoomed plot. Otherwise it is the zoom out point
% set by ZOOM RESET.
%
% When zoom is on, click the left mouse button to zoom in on the
% point under the mouse. Click the right mouse button to zoom out.
% Each time you click, the axes limits will be changed by a factor
% of 2 (in or out). You can also click and drag to zoom into an area.
% It is not possible to zoom out beyond the plots' current zoom out
% point. If ZOOM RESET has not been called the zoom out point is the
% original non-zoomed plot. If ZOOM RESET has been called the zoom out
% point is the zoom point that existed when it was called.
% Double clicking zooms out to the current zoom out point -
% the point at which zoom was first turned on for this figure
% (or to the point to which the zoom out point was set by ZOOM RESET).
% Note that turning zoom on, then off does not reset the zoom out point.
% This may be done explicitly with ZOOM RESET.
%
% ZOOM(FIG,OPTION) applies the zoom command to the figure specified
% by FIG. OPTION can be any of the above arguments.
% ZOOM FILL scales a plot such that it is as big as possible
% within the axis position rectangle for any azimuth and elevation.
% Clay M. Thompson 1-25-93
% Revised 11 Jan 94 by Steven L. Eddins
% Copyright 1984-2002 The MathWorks, Inc.
% $Revision: 5.63 $ $Date: 2002/04/08 21:44:39 $
% Note: zoom uses the figure buttondown and buttonmotion functions
%
% ZOOM XON zooms x-axis only
% ZOOM YON zooms y-axis only
%
% PARSE ARGS - set fig and zoomCommand
%
switch nargin
% no arg in
case 0
zoomCommand='toggle';
fig=get(0,'currentfigure');
if isempty(fig)
return
end
% one arg in
case 1
% If argument is string, it is a zoom command
% (i.e. (on, off, down, xdown, etc.).
if isstr(varargin{1})
zoomCommand=varargin{1};
% Otherwise, the argument is assumed to be a zoom factor.
else
scale_factor=varargin{1};
zoomCommand='scale';
end
fig=get(0,'currentfigure');
if isempty(fig)
return
end
% two arg in
case 2
fig=varargin{1};
if ~ishandle(fig)
error('First argument must be a figure handle.')
end
zoomCommand=varargin{2};
% too many args
otherwise
error(nargchk(0, 2, nargin));
end % switch nargin
%------------------------------------------------------------------------------%
zoomCommand=lower(zoomCommand);
%
% zoomCommand 'off'
%
if strcmp(zoomCommand,'off')
% turn zoom off
doZoomOff(fig);
scribefiglisten(fig,'off');
state = getappdata(fig,'ZOOMFigureState');
if ~isempty(state)
% since we didn't set the pointer,
% make sure it does not get reset
ptr = get(fig,'pointer');
% restore figure and non-uicontrol children
% don't restore uicontrols because they were restored
% already when zoom was turned on
uirestore(state,'nouicontrols');
set(fig,'pointer',ptr)
if isappdata(fig,'ZOOMFigureState')
rmappdata(fig,'ZOOMFigureState');
end
% get rid of on state appdata if it exists
% the non-existance of this appdata
% indicates that zoom is off.
if isappdata(fig,'ZoomOnState')
rmappdata(fig,'ZoomOnState');
end
end
% done, go home.
return
end
%---------------------------------------------------------------------------------%
%
% set some things we need for other zoomCommands
%
ax=get(fig,'currentaxes');
rbbox_mode = 0;
% initialize unconstrained state
zoomx = 1; zoomy = 1;
% catch 3d zoom
if ~isempty(ax) & any(get(ax,'view')~=[0 90]) ...
& ~(strcmp(zoomCommand,'scale')| ...
strcmp(zoomCommand,'fill'))
fZoom3d = 1;
else
fZoom3d = 0;
end
%----------------------------------------------------------------------------------%
%
% the zoomCommand is 'toggle'
%
if strcmp(zoomCommand,'toggle'),
state = getappdata(fig,'ZOOMFigureState');
if isempty(state)
zoom(fig,'on');
else
zoom(fig,'off');
end
% done, go home
return
end % if
%----------------------------------------------------------------------------------%
%
% Set/check a few more things before switching to one of remaining zoom actions
%
% Set zoomx,zoomy and zoomCommand for constrained zooms
if strcmp(zoomCommand,'xdown'),
zoomy = 0; zoomCommand = 'down'; % Constrain y
elseif strcmp(zoomCommand,'ydown')
zoomx = 0; zoomCommand = 'down'; % Constrain x
end
% Catch bad argin/argout match
if (nargout ~= 0) & ...
~isequal(zoomCommand,'getmode') & ...
~isequal(zoomCommand,'getlimits') & ...
~isequal(zoomCommand,'getconnect')
error(['ZOOM only returns an output if the command is getmode,' ...
' getlimits, or getconnect']);
end
%----------------------------------------------------------------------------------%
%
% Switch for rest of zoomCommands
%
switch zoomCommand
case 'down'
% Activate axis that is clicked in
allAxes = findall(datachildren(fig),'flat','type','axes');
ZOOM_found = 0;
% this test may be causing failures for 3d axes
for i=1:length(allAxes)
ax=allAxes(i);
ZOOM_Pt1 = get(ax,'CurrentPoint');
xlim = get(ax,'xlim');
ylim = get(ax,'ylim');
if (xlim(1) <= ZOOM_Pt1(1,1) & ZOOM_Pt1(1,1) <= xlim(2) & ...
ylim(1) <= ZOOM_Pt1(1,2) & ZOOM_Pt1(1,2) <= ylim(2))
ZOOM_found = 1;
set(fig,'currentaxes',ax);
break
end
end
if ZOOM_found==0
return
end
% Check for selection type
selection_type = get(fig,'SelectionType');
zoomMode = getappdata(fig,'ZOOMFigureMode');
axz = get(ax,'ZLabel');
if fZoom3d
viewData = getappdata(axz,'ZOOMAxesView');
if isempty(viewData)
viewProps = { 'CameraTarget'...
'CameraTargetMode'...
'CameraViewAngle'...
'CameraViewAngleMode'};
setappdata(axz,'ZOOMAxesViewProps', viewProps);
setappdata(axz,'ZOOMAxesView', get(ax,viewProps));
end
if isempty(zoomMode) | strcmp(zoomMode,'in');
zoomLeftFactor = 1.5;
zoomRightFactor = .75;
elseif strcmp(zoomMode,'out');
zoomLeftFactor = .75;
zoomRightFactor = 1.5;
end
switch selection_type
case 'open'
set(ax,getappdata(axz,'ZOOMAxesViewProps'),...
getappdata(axz,'ZOOMAxesView'));
case 'normal'
newTarget = mean(get(ax,'CurrentPoint'),1);
set(ax,'CameraTarget',newTarget);
camzoom(ax,zoomLeftFactor);
otherwise
newTarget = mean(get(ax,'CurrentPoint'),1);
set(ax,'CameraTarget',newTarget);
camzoom(ax,zoomRightFactor);
end
return
end
if isempty(zoomMode) | strcmp(zoomMode,'in');
switch selection_type
case 'normal'
% Zoom in
m = 1;
scale_factor = 2; % the default zooming factor
case 'open'
% Zoom all the way out
zoom(fig,'out');
return;
otherwise
% Zoom partially out
m = -1;
scale_factor = 2;
end
elseif strcmp(zoomMode,'out')
switch selection_type
case 'normal'
% Zoom partially out
m = -1;
scale_factor = 2;
case 'open'
% Zoom all the way out
zoom(fig,'out');
return;
otherwise
% Zoom in
m = 1;
scale_factor = 2; % the default zooming factor
end
else % unrecognized zoomMode
return
end
ZOOM_Pt1 = get_currentpoint(ax);
ZOOM_Pt2 = ZOOM_Pt1;
center = ZOOM_Pt1;
if (m == 1)
% Zoom in
units = get(fig,'units'); set(fig,'units','pixels')
rbbox([get(fig,'currentpoint') 0 0],get(fig,'currentpoint'),fig);
ZOOM_Pt2 = get_currentpoint(ax);
set(fig,'units',units)
% Note the currentpoint is set by having a non-trivial up function.
if min(abs(ZOOM_Pt1-ZOOM_Pt2)) >= ...
min(.01*[diff(get_xlim(ax)) diff(get_ylim(ax))]),
% determine axis from rbbox
a = [ZOOM_Pt1;ZOOM_Pt2]; a = [min(a);max(a)];
% Undo the effect of get_currentpoint for log axes
if strcmp(get(ax,'XScale'),'log'),
a(1:2) = 10.^a(1:2);
end
if strcmp(get(ax,'YScale'),'log'),
a(3:4) = 10.^a(3:4);
end
rbbox_mode = 1;
end
end
limits = zoom(fig,'getlimits');
case 'scale',
if all(get(ax,'view')==[0 90]), % 2D zooming with scale_factor
% Activate axis that is clicked in
ZOOM_found = 0;
ax = gca;
xlim = get(ax,'xlim');
ylim = get(ax,'ylim');
ZOOM_Pt1 = [sum(xlim)/2 sum(ylim)/2];
ZOOM_Pt2 = ZOOM_Pt1;
center = ZOOM_Pt1;
if (xlim(1) <= ZOOM_Pt1(1,1) & ZOOM_Pt1(1,1) <= xlim(2) & ...
ylim(1) <= ZOOM_Pt1(1,2) & ZOOM_Pt1(1,2) <= ylim(2))
ZOOM_found = 1;
end % if
if ZOOM_found==0, return, end
if (scale_factor >= 1)
m = 1;
else
m = -1;
end
else % 3D
old_CameraViewAngle = get(ax,'CameraViewAngle')*pi/360;
ncva = atan(tan(old_CameraViewAngle)*(1/scale_factor))*360/pi;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -