| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
Use the Zoom tools
on the figure toolbar
to zoom in or zoom out on a plot, or select Zoom In or Zoom
Out from the figure's Tools menu.
For details, see Enlarging the View in the MATLAB Graphics
documentation.
zoom on
zoom off
zoom out
zoom reset
zoom
zoom xon
zoom
yon
zoom(factor)
zoom(fig, option)
h = zoom(figure_handle)
zoom on turns on interactive zooming. When interactive zooming is enabled in a figure, pressing a mouse button while your cursor is within an axes zooms into the point or out from the point beneath the mouse. Zooming changes the axes limits. When using zoom mode, you
Zoom in by positioning the mouse cursor where you want the center of the plot to be and either
Press the mouse button or
Rotate the mouse scroll wheel away from you (upward).
Zoom out by positioning the mouse cursor where you want the center of the plot to be and either
Simultaneously press Shift and the mouse button, or
Rotate the mouse scroll wheel toward you (downward).
Each mouse click or scroll wheel click zooms in or out by a factor of 2.
Clicking and dragging over an axes when zooming in is enabled draws a rubberband box. When you release the mouse button, the axes zoom in to the region enclosed by the rubberband box.
Double-clicking over an axes returns the axes to its initial zoom setting in both zoom-in and zoom-out modes.
zoom off turns interactive zooming off.
zoom out returns the plot to its initial zoom setting.
zoom reset remembers the current zoom setting as the initial zoom setting. Later calls to zoom out, or double-clicks when interactive zoom mode is enabled, will return to this zoom level.
zoom toggles the interactive zoom status between off and on (restoring the most recently used zoom tool).
zoom xon and zoom yon set zoom on for the x- and y-axis, respectively.
zoom(factor) zooms in or out by the specified zoom factor, without affecting the interactive zoom mode. Values greater than 1 zoom in by that amount, while numbers greater than 0 and less than 1 zoom out by 1/factor.
zoom(fig, option) Any of the preceding options can be specified on a figure other than the current figure using this syntax.
h = zoom(figure_handle) returns a zoom mode object for the figure figure_handle for you to customize the mode's behavior.
Access the following properties of zoom mode objects via get and modify some of them using set.
Enable 'on'|'off' — Specifies whether this figure mode is currently enabled on the figure
FigureHandle <handle> — The associated figure handle, a read-only property that cannot be set
Motion 'horizontal'|'vertical'|'both' — The type of zooming enabled for the figure
Direction 'in'|'out' — The direction of the zoom operation
RightClickAction 'InverseZoom'|'PostContextMenu' — The behavior of a right-click action
A value of 'InverseZoom' causes a right-click to zoom out. A value of 'PostContextMenu' displays a context menu. This setting persists between MATLAB sessions.
UIContextMenu <handle> — Specifies a custom context menu to be displayed during a right-click action
This property is ignored if the RightClickAction property has been set to 'on'.
You can program the following callbacks for zoom mode operations.
ButtonDownFilter <function_handle> — Function to intercept ButtonDown events
The application can inhibit the zoom operation under circumstances the programmer defines, depending on what the callback returns. The input function handle should reference a function with two implicit arguments (similar to Handle Graphics object callbacks), as follows:
function [res] = myfunction(obj,event_obj) % obj handle to the object that has been clicked on % event_obj struct for event data (empty in this release) % res [output] a logical flag to determine whether the zoom % operation should take place or the 'ButtonDownFcn' % property of the object should take precedence
ActionPreCallback <function_handle> — Function to execute before zooming
Set this callback if you want to execute code when a zoom operation starts. The input function handle should reference a function with two implicit arguments (similar to Handle Graphics object callbacks), as follows:
function myfunction(obj,event_obj) % obj handle to the figure that has been clicked on. % event_obj object containing struct of event data
The event data has the following field.
Axes | The handle of the axes that is being zoomed |
ActionPostCallback <function_handle> — Function to execute after zooming
Set this callback if you want to execute code when a zoom operation finishes. The input function handle should reference a function with two implicit arguments (similar to Handle Graphics object callbacks), as follows:
function myfunction(obj,event_obj) % obj handle to the figure that has been clicked on % event_obj object containing struct of event data (same as the % event data of the 'ActionPreCallback' callback)
The following functions in zoom mode query and set certain of its properties.
flags = isAllowAxesZoom(h,axes) — Function querying permission to zoom axes
Calling the function isAllowAxesZoom on the zoom object, h, with a vector of axes handles, axes, as input returns a logical array of the same dimension as the axes handle vector, which indicates whether a zoom operation is permitted on the axes objects.
setAllowAxesZoom(h,axes,flag) — Function to set permission to zoom axes
Calling the function setAllowAxesZoom on the zoom object, h, with a vector of axes handles, axes, and a logical scalar, flag, either allows or disallows a zoom operation on the axes objects.
info = getAxesZoomMotion(h,axes) — Function to get style of zoom operations
Calling the function getAxesZoomMotion on the zoom object, H, with a vector of axes handles, axes, as input returns a character cell array of the same dimension as the axes handle vector, which indicates the type of zoom operation for each axes. Possible values for the type of operation are 'horizontal', 'vertical', or 'both'.
setAxesZoomMotion(h,axes,style) — Function to set style of zoom operations
Calling the function setAxesZoomMotion on the zoom object, h, with a vector of axes handles, axes, and a character array, style, ses the style of zooming on each axes.
Plot a graph and turn on Zoom mode:
plot(1:10); zoom on % zoom in on the plot
Create zoom mode object and constrain to x-axis zooming:
plot(1:10); h = zoom; set(h,'Motion','horizontal','Enable','on'); % zoom in on the plot in the horizontal direction.
Create four axes as subplots and set zoom style differently for each by setting a different property for each axes handle:
ax1 = subplot(2,2,1); plot(1:10); h = zoom; ax2 = subplot(2,2,2); plot(rand(3)); setAllowAxesZoom(h,ax2,false); ax3 = subplot(2,2,3); plot(peaks); setAxesZoomMotion(h,ax3,'horizontal'); ax4 = subplot(2,2,4); contour(peaks); setAxesZoomMotion(h,ax4,'vertical'); % Zoom in on the plots.
Create a buttonDown callback for zoom mode objects to trigger. Copy the following code to a new M-file, execute it, and observe zooming behavior:
function demo
% Allow a line to have its own 'ButtonDownFcn' callback.
hLine = plot(rand(1,10));
set(hLine,'ButtonDownFcn','disp(''This executes'')');
set(hLine,'Tag','DoNotIgnore');
h = zoom;
set(h,'ButtonDownFilter',@mycallback);
set(h,'Enable','on');
% mouse click on the line
%
function [flag] = mycallback(obj,event_obj)
% If the tag of the object is 'DoNotIgnore', then return true.
objTag = get(obj,'Tag');
if strcmpi(objTag,'DoNotIgnore')
flag = true;
else
flag = false;
end
Create callbacks for pre- and post-buttonDown events for zoom mode objects to trigger. Copy the following code to a new M-file, execute it, and observe zoom behavior:
function demo
% Listen to zoom events
plot(1:10);
h = zoom;
set(h,'ActionPreCallback',@myprecallback);
set(h,'ActionPostCallback',@mypostcallback);
set(h,'Enable','on');
%
function myprecallback(obj,evd)
disp('A zoom is about to occur.');
%
function mypostcallback(obj,evd)
newLim = get(evd.Axes,'XLim');
msgbox(sprintf('The new X-Limits are [%.2f %.2f].',newLim));
Coding a context menu that lets the user to switch to Pan mode by right-clicking:
figure;plot(magic(10))
hCMZ = uicontextmenu;
hZMenu = uimenu('Parent',hCMZ,'Label','Switch to pan','Callback','pan(gcbf,''on'')');
hZoom = zoom(gcf);
set(hZoom,'UIContextMenu',hCMZ);
zoom('on')You cannot add items to the built-in zoom context menu, but you can replace it with your own.
zoom changes the axes limits by a factor of 2 (in or out) each time you press the mouse button while the cursor is within an axes. You can also click and drag the mouse to define a zoom area, or double-click to return to the initial zoom level.
You can create a zoom mode object once and use it to customize the behavior of different axes, as Example 3 illustrates. You can also change its callback functions on the fly.
Note Do not change figure callbacks within an interactive mode. While a mode is active (when panning, zooming, etc.), you will receive a warning if you attempt to change any of the figure's callbacks and the operation will not succeed. The one exception to this rule is the figure WindowButtonMotionFcn callback, which can be changed from within a mode. Therefore, if you are creating a GUI that updates a figure's callbacks, the GUI should some keep track of which interactive mode is active, if any, before attempting to do this. |
When you assign different zoom behaviors to different subplot axes via a mode object and then link them using the linkaxes function, the behavior of the axes you manipulate with the mouse carries over to the linked axes, regardless of the behavior you previously set for the other axes.
Object Manipulation for related functions
![]() | zip |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |