Dynamically reading cursor location
    15 views (last 30 days)
  
       Show older comments
    
    Ranjan Sonalkar
      
 on 23 Jun 2020
  
    
    
    
    
    Commented: Ameer Hamza
      
      
 on 25 Jun 2020
            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?
0 Comments
Accepted Answer
  Ameer Hamza
      
      
 on 23 Jun 2020
        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 Comments
  Ameer Hamza
      
      
 on 25 Jun 2020
				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
More Answers (0)
See Also
Categories
				Find more on Data Exploration in Help Center and File Exchange
			
	Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
