Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB   

zoom - Turn zooming on or off or magnify by factor

GUI Alternatives

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.

Syntax

zoom on
zoom off
zoom out
zoom reset
zoom
zoom xon
zoom yon
zoom(factor)
zoom(fig, option)
h = zoom(figure_handle)

Description

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

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.

Using Zoom Mode Objects

Access the following properties of zoom mode objects via get and modify some of them using set.

Zoom Mode Callbacks

You can program the following callbacks for zoom mode operations.

Zoom Mode Utility Functions

The following functions in zoom mode query and set certain of its properties.

Examples

Example 1 — Entering Zoom Mode

Plot a graph and turn on Zoom mode:

plot(1:10);
zoom on
% zoom in on the plot

Example 2 — Constrained Zoom

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.

Example 3 — Constrained Zoom in Subplots

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.

Example 4 — Coding a ButtonDown Callback

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

Example 5 — Coding Pre- and Post-Callback Behavior

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));

Example 6 — Creating a Context Menu for Zoom Mode

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.

Remarks

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.

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.

See Also

linkaxes, pan, rotate3d

Object Manipulation for related functions

  


Recommended Products

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