
Scrolling and zooming with mouse wheel
51 views (last 30 days)
Show older comments
Hi,
I'm programming a GUI programmatically (not using GUIDE) and want to scroll a slider to scroll through variables.
The GUI also has an axes which to zoom on request.
I can't use the scrolling of my slider when the zoom is active (or panning, data tip and so on).
Is there a possibility for a workaround or that the zoom only works for the axes?
I managed to scroll the slider by using the current position of the mouse. When the mouse on the slider the scrolling with the mouse wheel is active. It would be nice if this is also possible for the zooming.
This is a simplified codes I use:
function test_figscroll
fig=figure('Position',[200 200 1000 800],'Resize','off','Units','Pixel',...
'Paperunits','centimeters','PaperSize',[30 20],'PaperPosition',[0 0 30 20]);
ax=axes('units','pixel','Position',[70,50,690,680],'parent',fig);
ax.Box='on';
ax.XGrid='on';
ax.YGrid='on';
buttonzoom=uicontrol('Style','togglebutton','Position',[90,765,30,30],'callback',@zooming);
hTempSlide=uicontrol('Style','slider','Position',[800,50,40,550]);
hTempSlide.Min=1;
hTempSlide.Max=10;
hTempSlide.Value=10;
function zooming(~,~)
hz=zoom(ax);
hz.RightClickAction='InverseZoom';
if buttonzoom.Value
hz.Enable='on';
hz.Motion='both';
else
hz.Enable='off';
end
end
function figscroll(~,value)
mousePoint=get(fig,'CurrentPoint');
if mousePoint(1)<840 && mousePoint(1)>800 && mousePoint(2)<600 && mousePoint(2)>50
if value.VerticalScrollCount>0 && hTempSlide.Value>hTempSlide.Min
hTempSlide.Value=hTempSlide.Value-1;
if hTempSlide.Value<hTempSlide.Min
hTempSlide.Value=hTempSlide.Min;
end
elseif value.VerticalScrollCount<0 && hTempSlide.Value<hTempSlide.Max
hTempSlide.Value=hTempSlide.Value+1;
if hTempSlide.Value>hTempSlide.Max
hTempSlide.Value=hTempSlide.Max;
end
end
end
end
end
I'm using Matlab R2015a SP1 as it is the last version with 32 bit compatibility.
0 Comments
Accepted Answer
Aravind
on 13 Mar 2025
From your question, it appears you are developing a MATLAB GUI and encountering an issue where the slider's scroll functionality is being overridden by the zoom feature of the axes when activated and want a solution or workaround that allows the slider to remain scrollable regardless of whether zoom or other axis functionality is enabled.
To achieve this, you can utilize MATLAB's event handling to dynamically enable or disable zoom based on the mouse position. The key is to use the "WindowButtonMotionFcn" to continuously monitor the cursor's position. More information on "WindowButtonMotionFcn" can be found here: https://www.mathworks.com/help/matlab/ref/matlab.ui.figure-properties.html#buiwuyk-1-WindowButtonMotionFcn.
Using this approach, you can detect if the cursor is over the axes or the slider and adjust the functionalities accordingly. When the cursor is over the axes, the zoom functionality is activated if the corresponding toggle button is pressed. If the cursor is elsewhere, zoom is disabled, allowing the slider to respond to scroll events.
Below is a code snippet to implement this:
function test_figscroll
fig = figure('Position', [200 200 1000 800], 'Resize', 'off', 'Units', 'Pixels', ...
'PaperUnits', 'centimeters', 'PaperSize', [30 20], 'PaperPosition', [0 0 30 20]);
ax = axes('Units', 'pixels', 'Position', [70, 50, 690, 680], 'Parent', fig);
ax.Box = 'on';
ax.XGrid = 'on';
ax.YGrid = 'on';
buttonzoom = uicontrol('Style', 'togglebutton', 'Position', [90, 765, 30, 30], 'Callback', @zooming);
hTempSlide = uicontrol('Style', 'slider', 'Position', [800, 50, 40, 550]);
hTempSlide.Min = 1;
hTempSlide.Max = 10;
hTempSlide.Value = 10;
% Set the figure's scroll wheel function to our custom figscroll
fig.WindowScrollWheelFcn = @figscroll;
% Set the motion function to check the mouse position
fig.WindowButtonMotionFcn = @checkMousePosition;
function checkMousePosition(~, ~)
mousePoint = get(fig, 'CurrentPoint');
if mousePoint(1) > 70 && mousePoint(1) < 760 && mousePoint(2) > 50 && mousePoint(2) < 730
% Mouse is over the axes
if buttonzoom.Value
% Enable zoom only when the toggle button is active
set(fig, 'WindowScrollWheelFcn', '');
zoom(ax, 'on');
end
else
% Mouse is outside the axes
zoom(ax, 'off');
set(fig, 'WindowScrollWheelFcn', @figscroll);
end
end
function zooming(~, ~)
% Initialize the zoom object for the axes
hz = zoom(ax);
hz.RightClickAction = 'InverseZoom'; % Set right-click action for zoom-out
if buttonzoom.Value
hz.Enable = 'on';
hz.Motion = 'both';
else
hz.Enable = 'off';
end
end
function figscroll(~, event)
mousePoint = get(fig, 'CurrentPoint');
if mousePoint(1) < 840 && mousePoint(1) > 800 && mousePoint(2) < 600 && mousePoint(2) > 50
if event.VerticalScrollCount > 0 && hTempSlide.Value > hTempSlide.Min
hTempSlide.Value = hTempSlide.Value - 1;
elseif event.VerticalScrollCount < 0 && hTempSlide.Value < hTempSlide.Max
hTempSlide.Value = hTempSlide.Value + 1;
end
end
end
end
In this code, the mouse's position is continuously monitored, and the zoom functionality is enabled only when the toggle button is active and the mouse is over the axes. This setup prevents the zoom from interfering with the slider’s scrolling capability. The “WindowScrollWheelFcn” is customized to respond to scroll events only when the mouse is over the slider, allowing the slider to adjust as needed. More information on "WindowScrollWheelFcn" can be found here: https://www.mathworks.com/help/matlab/ref/matlab.ui.figure-properties.html#buiwuyk-1-WindowScrollWheelFcn.
The result obtained using the above code is as shown below:

This setup should help you achieve separate and non-interfering functionalities for zoom and slider interactions, ensuring a more intuitive experience in your GUI application.
I hope this resolves your issue.
0 Comments
More Answers (0)
See Also
Categories
Find more on Migrate GUIDE Apps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!