I am using ginput to select a location within a plot. Is there a function that would allow me to access the current location of the cursor as I move it (before pressing the mouse) and display the coordinates, something like how the current lat/long of the cursor position are displayed on Google Earth?

 採用された回答

Ameer Hamza
Ameer Hamza 2020 年 6 月 23 日

0 投票

You can use the WindowButtonMotionFcn callback of the figure object. Run the following example
fig = figure();
ax = axes(fig);
fig.WindowButtonMotionFcn = {@mouseMotionCB, ax};
function mouseMotionCB(fig, event, ax_handle)
fprintf('Current Point is %f %f %f\n', ax_handle.CurrentPoint(1,1:3));
end

5 件のコメント

Ranjan Sonalkar
Ranjan Sonalkar 2020 年 6 月 23 日
Ameer
Thanks for pointing to this function, which should lead to the solution.
This function appears to activate whenever the cursor is within the confines of the figure. I want to restrict it to operate only when it is within one of the plots within the figure (there are two) and also when there is a specific command from the program to select a point within that scale. Is there a way I can do that?
Ameer Hamza
Ameer Hamza 2020 年 6 月 24 日
See the following example. You can adapt it according to your requirement.
fig = figure();
ax1 = subplot(1,2,1);
ax2 = subplot(1,2,2);
fig.WindowButtonMotionFcn = {@mouseMotionCB, ax1, ax2};
function mouseMotionCB(fig, event, ax1, ax2)
currentPoint1 = ax1.CurrentPoint(1,1:3);
x1 = currentPoint1(1);
y1 = currentPoint1(2);
if (ax1.XLim(1)<x1)&&(x1<ax1.XLim(2)) && (ax1.YLim(1)<y1)&&(y1<ax1.YLim(2))
fprintf('This is axes 1, Current Point is %f %f %f\n', currentPoint1);
end
currentPoint2 = ax2.CurrentPoint(1,1:3);
x2 = currentPoint2(1);
y2 = currentPoint2(2);
if (ax2.XLim(1)<x2)&&(x2<ax2.XLim(2)) && (ax2.YLim(1)<y2)&&(y2<ax2.YLim(2))
fprintf('This is axes 2, Current Point is %f %f %f\n', currentPoint2);
end
end
Ranjan Sonalkar
Ranjan Sonalkar 2020 年 6 月 24 日
Ameer
Sorry to bother you again.
I have the call to {@mouseMotionCB, ax1, ax2} just prior to a "ginput" call. I would like the mouseMotionCB function to work while I move the cursor around the scale as the script waits for the mouseclick input from ginput. But, the dislay does not show anything while the script is waiting for the mouse-click response from ginput. If I put a break point in the script either before or after the ginput call, the display shows the coordinates dynamically as I move the cursor during the break point pause. What can I do to see the mouse coordinates during the pause for ginput?
Thank you very much for your help.
Ranjan Sonalkar
Ranjan Sonalkar 2020 年 6 月 24 日
編集済み: Ranjan Sonalkar 2020 年 6 月 24 日
I put in a call to waitforbuttonpress just prior to ginput. So, now the mouse position shows dynamically up as the cursor is moved. When the user finds the desired location on the scale, a double-click of the mouse (first to resume after the waitforbuttonpress call, and the second as a response to ginput at the desired location) appeears to be the acceptable solution.
Ameer Hamza
Ameer Hamza 2020 年 6 月 25 日
If you want to use ginput(), then an alternative solution is to use a timer() objects to detect the values. In this case, you do not need to double click the axes object.
fig = figure('Interruptible', 'off');
ax1 = subplot(1,2,1);
ax2 = subplot(1,2,2);
fig.WindowButtonMotionFcn = @(~,~) [];
t = timer('Period', 0.1, 'ExecutionMode', 'fixedRate', 'TimerFcn', {@mouseMotionCB, ax1, ax2});
start(t)
ginput(1)
stop(t);
delete(t);
function mouseMotionCB(fig, event, ax1, ax2)
currentPoint1 = ax1.CurrentPoint(1,1:3);
x1 = currentPoint1(1);
y1 = currentPoint1(2);
if (ax1.XLim(1)<x1)&&(x1<ax1.XLim(2)) && (ax1.YLim(1)<y1)&&(y1<ax1.YLim(2))
fprintf('This is axes 1, Current Point is %f %f %f\n', currentPoint1);
end
currentPoint2 = ax2.CurrentPoint(1,1:3);
x2 = currentPoint2(1);
y2 = currentPoint2(2);
if (ax2.XLim(1)<x2)&&(x2<ax2.XLim(2)) && (ax2.YLim(1)<y2)&&(y2<ax2.YLim(2))
fprintf('This is axes 2, Current Point is %f %f %f\n', currentPoint2);
end
end

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Exploration についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by