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:

Enable 'on'|'off'

Specifies whether this figure mode is currently enabled on the figure.

FigureHandle <handle>

The associated figure handle. This read-only property 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.

ButtonDownFilter <function_handle>

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 callbacks), as follows:

function [res] = myfunction(obj,event_obj)
% OBJ        handle to the object that has been clicked on.
% EVENT_OBJ  handle to event object (empty in this release).
% RES        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>

Set this callback to listen to when a zoom operation starts. The input function handle should reference a function with two implicit arguments (similar to handle callbacks), as follows:

function myfunction(obj,event_obj)
% obj         handle to the figure that has been clicked on.
% event_obj   handle to event object.

The event object has the following read-only property:

Axes

The handle of the axes that is being zoomed

ActionPostCallback <function_handle>

Set this callback to listen to when a zoom operation finishes. The input function handle should reference a function with two implicit arguments (similar to handle callbacks), as follows:

function myfunction(obj,event_obj)
% obj         handle to the figure that has been clicked on.
% event_obj   handle to event object. The object has the same
%             properties as the event_obj of the
%             'ActionPreCallback' callback.

UIContextMenu <handle>

Specifies a custom context menu to be displayed during a right-click action. This property is ignored if the 'RightClickZoomOut' property has been set to 'on'.

flags = isAllowAxesZoom(h,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)

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)

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)

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.

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

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS