zoom on the mouse pointer

13 views (last 30 days)
Luca Tognetti
Luca Tognetti on 19 Jan 2023
Answered: Kartik on 20 Mar 2023
Hi, I made an app on the app Designer and I created a function for zooming on a plot using the location of the mouse pointer. Here is how it works and the script of the function:
function UIFigureWindowButtonMotion(app, event)
pointerLocation = get(app.UIFigure,'CurrentPoint'); %location of pointer
axisYcoords = [app.UIAxes.InnerPosition(2),...
app.UIAxes.InnerPosition(2) + app.UIAxes.InnerPosition(4)]; %UIAxes y coordinates
axisXcoords = [app.UIAxes.InnerPosition(1),...
app.UIAxes.InnerPosition(1) + app.UIAxes.InnerPosition(3)]; %UIAxes x coordinates
%if you're pointing inside the UIAxes and if there is data in the UIAxes.
if pointerLocation(1) > axisXcoords(1) &&...
pointerLocation(1) < axisXcoords(2) &&...
pointerLocation(2) > axisYcoords(1) &&...
pointerLocation(2) < axisYcoords(2) &&...
~isempty(app.UIAxes.Children)
ZoomMag = 10; %zoom magnification
xCalibrate = (app.UIAxes.XLim(2) - app.UIAxes.XLim(1))/...
app.UIAxes.InnerPosition(3); %x value per UIAxes inner pixel
yCalibrate = (app.UIAxes.YLim(2) - app.UIAxes.YLim(1))/...
app.UIAxes.InnerPosition(4); %y value per UIAxes inner pixel
%find what your x and y value are at your cursor within the UIAxes
xValue = xCalibrate*(pointerLocation(1)-app.UIAxes.InnerPosition(1));
yValue = yCalibrate*(pointerLocation(2)-app.UIAxes.InnerPosition(2));
[A, map] = imread(app.fullfilename);
A = flipud(A);
%plot the pointer location as a blue dot and plot the original data on UIAxes2
hold(app.UIAxes,'on');
h = findobj(app.UIAxes2,'Type','line');
delete(h);
image([A, map], 'parent', app.UIAxes2);
set(app.UIAxes2,'YDir','normal')
plot(app.UIAxes2, xValue, yValue, 'b.', 'MarkerSize', 15);
pause(0.0000000000000000001);
%adjust the x and y limits on UIAxes2
xlim(app.UIAxes2,...
[xValue - (app.UIAxes.XLim(2) - app.UIAxes.XLim(1))/ZoomMag,...
xValue + (app.UIAxes.XLim(2) - app.UIAxes.XLim(1))/ZoomMag])
ylim(app.UIAxes2,...
[yValue - (app.UIAxes.YLim(2) - app.UIAxes.YLim(1))/ZoomMag,...
yValue + (app.UIAxes.YLim(2) - app.UIAxes.YLim(1))/ZoomMag])
end
end
in this case it all worked fine, but then I insert the line "axis(app.UIAxes, 'equal');" to maintain the original ratio of the image and the X value in the zoomed area is now shifted to the right. In the second image I was pointing at 100 on the X axis and it shows me the right end of the plot. I need to recalibrate the xValue, but I don't know where to insert some changes.
  3 Comments
Luca Tognetti
Luca Tognetti on 19 Jan 2023
thanks for the quick reply, but this didn't help me that much... what you suggest to implement?

Sign in to comment.

Answers (1)

Kartik
Kartik on 20 Mar 2023
Hi,
To recalibrate the 'xValue' after setting the aspect ratio of the axes to 'equal', you need to modify the 'xCalibrate' variable to include the aspect ratio adjustment. You can calculate the aspect ratio of the axes by dividing the width by the height of the 'UIAxes' 'InnerPosition' property, and then adjust 'xCalibrate' accordingly. Here's how you can modify the 'xCalibrate' calculation:
% calculate the aspect ratio of the axes
aspectRatio = app.UIAxes.InnerPosition(3)/app.UIAxes.InnerPosition(4);
% adjust xCalibrate to account for the aspect ratio
xCalibrate = (app.UIAxes.XLim(2) - app.UIAxes.XLim(1))/(app.UIAxes.InnerPosition(3)*aspectRatio);
Replace the existing 'xCalibrate' calculation in your code with the above lines, and it should recalculate 'xValue' correctly after setting the aspect ratio.

Categories

Find more on Data Exploration in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!