zoom

Turn zooming on or off or magnify by factor

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 zoom mode. When zoom mode 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 off zoom mode. Starting in R2018b, some zoom interactions are enabled by default, regardless of the zoom mode. If you want to disable these default interactions, then use the disableDefaultInteractivity function.

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 zoom mode 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

You can access the following properties.

  • 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. This property only affects axes in a 2-D view ([0 90]).

  • 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.

  • ContextMenu <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 'InverseZoom'.

  • UseLegacyExplorationModes 'off' (default) | on/off logical value – Legacy mode, specified as 'on' or 'off', or as numeric or logical 1 (true) or 0 (false). A value of 'on' is equivalent to true, and 'off' is equivalent to false. Thus, you can use the value of this property as a logical value. The value is stored as an on/off logical value of type matlab.lang.OnOffSwitchState.

    Setting this property to 'on' changes the behavior of interaction modes in UI figures so they match the behavior of modes in traditional figures. For more information, see enableLegacyExplorationModes. Once this property is set to 'on', it cannot be changed back to 'off'. This property only applies to zoom objects for figures created using the uifigure function or in MATLAB Online™.

Zoom Mode Callbacks

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

    function [res] = myfunction(obj,event_obj)
    % obj          handle to the object clicked on
    % event_obj    struct for event data (empty in this release)
    % res [output] a logical flag determines whether the zoom
    %              operation should take place(for 'res' set
    %              to 'false' or the 'ButtonDownFcn' property
    %              of the object should take precedence (when
    %              'res' is 'true')
    
  • 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 graphics object callbacks), as follows:

    function myfunction(obj,event_obj)
    % obj         handle to the figure 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 graphics object callbacks), as follows:

    function myfunction(obj,event_obj)
    % obj         handle to the figure clicked on
    % event_obj   object containing struct of event data 
    %             (same as the event data of the 
    %             'ActionPreCallback' callback)

Zoom Mode Utility Functions

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

  • flags = isAllowAxesZoom(h,ax) — Function querying permission to zoom axes.

    Calling the function isAllowAxesZoom on the zoom object, h, with a vector of axes handles, ax, 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,ax,flag) — Function to set permission to zoom axes.

    Calling the function setAllowAxesZoom on the zoom object, h, with a vector of axes handles, ax, and a logical scalar, flag, either allows or disallows a zoom operation on the axes objects.

  • cn = getAxesZoomConstraint(h,ax) — Function to get constraints of zoom operations.

    Calling the function getAxesZoomConstraint on the zoom object, h, with an axes object, ax, as input returns the constraint for the axes. The returned constraint is one of these values: 'x', 'y', 'z', 'xy', 'xz', 'yz', or 'unconstrained'.

  • setAxesZoomConstraint(h,ax,cnstr) — Function to set constraints of zoom operations.

    Calling the function setAxesZoomConstraint on the zoom object, h, with an axes object, ax, and a constraint option, cnstr, sets the constraint for the axes. Specify the constraint as one of these values: 'x', 'y', 'z', 'xy', 'xz', 'yz', or 'unconstrained'.

  • sty = getAxes3DPanAndZoomStyle(h,ax) — Function to get style of zoom operations.

    Calling the function getAxes3DPanAndZoomStyle on the zoom object, h, with a vector of axes handles, ax, as input returns the style of zooming for each axes. The returned value for each axes is either 'limits' or 'camera'.

  • setAxes3DPanAndZoomStyle(h,ax,style) — Function to set style of zoom operations.

    Calling the function setAxes3DPanAndZoomStyle on the zoom object, h, with a vector of axes handles, ax, and a character array, style, sets the style of zooming on each axes. Specify the style as either 'limits' or 'camera'.

  • cns = getAxesZoomMotion(h,ax) — Function to get constraints of zoom operations (not recommended, use getAxesZoomConstraint).

    Calling the function getAxesZoomMotion on the zoom object, h, with a vector of axes objects, ax, as input returns a character cell array of the same dimension as ax, which indicates the constraint for each axes. The returned value for each axes is 'horizontal', 'vertical' or 'both'.

  • setAxesZoomMotion(h,ax,constraints) — Function to set constraints of zoom operations (not recommended, use setAxesZoomConstraint).

    Calling the function setAxesZoomMotion on the zoom object, h, with a vector of axes objects, ax, and a character array, style, sets the constraint for each axes. Specify the constraints as 'horizontal', 'vertical' or 'both'.

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. Use dot notation to set properties.

plot(1:10);
h = zoom;
h.Motion = 'horizontal';
h.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 file, execute it, and observe zooming behavior. Use dot notation to set properties.

function demo
% Allow a line to have its own 'ButtonDownFcn' callback.
hLine = plot(rand(1,10));
hLine.ButtonDownFcn = 'disp(''This executes'')';
hLine.Tag = 'DoNotIgnore';
h = zoom;
h.ButtonDownFilter = @mycallback;
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 = 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 file, execute it, and observe zoom behavior. Use dot notation to set properties.

function demo
% Listen to zoom events
plot(1:10);
h = zoom;
h.ActionPreCallback = @myprecallback;
h.ActionPostCallback = @mypostcallback;
h.Enable = 'on';
%
function myprecallback(obj,evd)
disp('A zoom is about to occur.');
%
function mypostcallback(obj,evd)
newLim = 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. Use dot notation to set properties.

figure
plot(magic(10))
hCMZ = uicontextmenu;
hZMenu = uimenu('Parent',hCMZ,'Label','Switch to pan',...
'Callback','pan(gcbf,''on'')');
hZoom = zoom(gcf);
hZoom.ContextMenu = hCMZ;
zoom('on')
You cannot add items to the built-in zoom context menu, but you can replace it with your own.

Tips

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 UI that updates a figure's callbacks, the UI should somehow 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.

Alternatives

Use the Zoom tools in the toolbar to zoom in or zoom out on a plot, or select Zoom In or Zoom Out from the figure's Tools menu.

Compatibility Considerations

expand all

Not recommended starting in R2020a

See Also

| |

Introduced before R2006a