필터 지우기
필터 지우기

zoom on the mouse pointer

조회 수: 4 (최근 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에서 Specifying Target for Graphics Output에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by