MATLAB: Dynamically reading cursor location

cursor locationginputMATLAB

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?

Best Answer

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