How do I turn off plot zooming when mouse is moved?

Sometimes when I'm manipulating a plot with a mouse (e.g. zoom in/out, pan, zoom again, etc.), the zoom function starts to operate just on my mouse movements rather than the scroll wheel. If I move the mouse right, the plot zooms in; if I move the mouse left, the plot zooms out. How do I turn that off and set the zoom to work off of the scroll wheel again? I'm using MATLAB R2024b.

Answers (1)

hello
funny, I don't have this effect on my R2025a . Is it a matlab issue or a problem with your mouse ?
either I use the already available zoom/pan functions of the figure, or you can try with the script below if that works better for you
FYI, this is also a very powerful plotting tool :
hope it helps
% here’s a self-contained MATLAB script that enables smooth mouse-wheel zoom and click-drag pan for any figure, just like modern image viewers.
% This works for plots and images without requiring extra toolboxes.
%
% Example with a plot
x = 0:0.01:10;
plot(x, sin(x));
smoothZoomPan;
% Features
%
% Mouse wheel zoom centered on cursor position.
% Click-drag pan in any direction.
% Works for images and plots.
% No toolboxes required.
% Can be reused in any figure.
% MATLAB Script: smoothZoomPan.
function smoothZoomPan()
% SMOOTHZOOMPAN - Enables mouse-wheel zoom and click-drag pan
% Works for any MATLAB figure with axes or images.
%
% Usage:
% smoothZoomPan; % Run after creating your plot/image
fig = gcf; % Current figure
ax = gca; % Current axes
% Enable mouse wheel zoom
fig.WindowScrollWheelFcn = @(src, event) scrollZoom(ax, event);
% Enable click-drag pan
fig.WindowButtonDownFcn = @(src, event) startPan(fig, ax);
fig.WindowButtonUpFcn = @(src, event) stopPan(fig);
disp('Smooth zoom & pan enabled. Use mouse wheel to zoom, drag to pan.');
end
%% --- Helper Functions ---
function scrollZoom(ax, event)
% Get current mouse point in axes coordinates
cp = ax.CurrentPoint;
xCenter = cp(1,1);
yCenter = cp(1,2);
% Zoom factor (scroll up = zoom in, scroll down = zoom out)
zoomFactor = 1 - event.VerticalScrollCount * 0.1;
% Get current limits
xlimOld = xlim(ax);
ylimOld = ylim(ax);
% Calculate new limits centered on mouse position
xlimNew = xCenter + (xlimOld - xCenter) * zoomFactor;
ylimNew = yCenter + (ylimOld - yCenter) * zoomFactor;
% Apply new limits
xlim(ax, xlimNew);
ylim(ax, ylimNew);
end
function startPan(fig, ax)
% Store initial point and limits
fig.UserData.PanStartPoint = get(fig, 'CurrentPoint');
fig.UserData.OrigXLim = xlim(ax);
fig.UserData.OrigYLim = ylim(ax);
% Set motion callback
fig.WindowButtonMotionFcn = @(src, event) doPan(fig, ax);
end
function doPan(fig, ax)
% Get current mouse position
cp = get(fig, 'CurrentPoint');
startPt = fig.UserData.PanStartPoint;
% Calculate movement in figure units
dx = cp(1) - startPt(1);
dy = cp(2) - startPt(2);
% Convert movement to axis units
xRange = diff(fig.UserData.OrigXLim);
yRange = diff(fig.UserData.OrigYLim);
% Adjust limits
xlim(ax, fig.UserData.OrigXLim - dx * xRange / fig.Position(3));
ylim(ax, fig.UserData.OrigYLim + dy * yRange / fig.Position(4));
end
function stopPan(fig)
% Remove motion callback
fig.WindowButtonMotionFcn = '';
end

Categories

Products

Release

R2024b

Tags

Asked:

on 5 Jan 2026

Answered:

on 6 Jan 2026

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!