File Exchange

image thumbnail

Display Cursor Location

version 1.0.0.0 (2.36 KB) by Rodney Thomson
Display cursor coordinates within a plot axes window

8 Downloads

Updated 30 Jul 2008

View License

Display the current cursor coordinates within the bounds of a plot axes.

Assigned as a WindowButtonMotionFcn callback function. Only updates when mouse is moved over plot contents.

To use (See screenshot):

t = linspace(-5,5);
y = sinc(t);
f = figure;
plot(t, y, 'r');

set(f, 'WindowButtonMotionFcn', @(obj, event)cursorLocation(obj, event, 'BottomLeft', ' X: %.3f\n Y: %.3f', 'r'))

For more information:
http://iheartmatlab.blogspot.com/2008/07/display-cursor-coordinates.html

Cite As

Rodney Thomson (2020). Display Cursor Location (https://www.mathworks.com/matlabcentral/fileexchange/20892-display-cursor-location), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (10)

Mukundettan

Dear Rodney,

Thank you so much !! Working very well..

With best regards.

juan pedrosa

Hi, could you give me any pointers to make your function work when one of the axis is a datetime type?

Error while evaluating Figure WindowButtonMotionFcn.

Error using cursorLocation>getBounds (line 110)
Comparison is not defined between double and datetime arrays.

Error in cursorLocation (line 55)
[x_lims y_lims in_bounds] = getBounds(axes_handle, position);

James

Rodney,

Is there a way to allow this code to work and give output with subplots (or separate axes in one figure)? (e.g. subplot(211);plot(t,y);subplot(212);plot(t,t)

Rodney Thomson

Hi Aldo,

Can you check to make sure that cursorLocation.m is in your MATLAB path? If you type "which cursorLocation" in the MATLAB command window, it should return the location of cursorLocation if your path is set correctly.

Aldo Duron

Hi! Well... I have an issue, when I try to run the code exactly the way it's displayed, the code plots Figure 1 as shown in the screenshot, but it returns the error:

Undefined function 'cursorLocation' for input arguments of type 'double'.

Error in @(obj,event)cursorLocation(obj,event,'BottomLeft',' X: %.3f\n Y: %.3f','r')


Error while evaluating figure WindowButtonDownFcn

Error using edit (line 66)
Undefined function or variable 'obj'.

Error in helpUtils.errorDocCallback (line 23)
if ~edit(editTopic)

james qiu

very usefully ! thanks you very much!

Leslie

works fine
Thanks

Rodney Thomson

Eduardo, for a simple hack you could replace 'WindowButtonMotionFcn' in the above example with 'WindowButtonDownFcn' and it will respond to button clicks (both left and right) but only when not in zoom/pan mode.

Great function. Is there any way to use it to get the position when the mouse left button is clicked over the image? as in ginput function, to capture several cursor positions. thanks

Diablo

Thank you so much for your submission it really helps with my analysis of my plots!. Keep up on the great work!!!

MATLAB Release Compatibility
Created with R14SP3
Compatible with any release
Platform Compatibility
Windows macOS Linux
Acknowledgements

Inspired: stackobj, dtmcursor

Community Treasure Hunt

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

Start Hunting!