zoom on the mouse pointer

13 次查看(过去 30 天)
Luca Tognetti
Luca Tognetti 2023-1-19
回答: Kartik 2023-3-20
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 个评论
Luca Tognetti
Luca Tognetti 2023-1-19
thanks for the quick reply, but this didn't help me that much... what you suggest to implement?

请先登录,再进行评论。

回答(1 个)

Kartik
Kartik 2023-3-20
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.

类别

Help CenterFile Exchange 中查找有关 Data Exploration 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by