No License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Trace the Position of Cursor without Click

4.9 | 8 ratings Rate this file 6 Downloads (last 30 days) File Size: 2.53 KB File ID: #4170 Version: 1.1

Trace the Position of Cursor without Click


Kang Zhao (view profile)


18 Nov 2003 (Updated )

It works like GINPUT provided by MATLAB,but traces the position of cursor without click ...

| Watch this File

File Information

function [x,y]=gpos(h_axes)
%GPOS Get current position of cusor and return its coordinates in axes with handle h_axes
% h_axes - handle of specified axes
% [x,y] - cursor coordinates in axes h_aexs
% -------------------------------------------------------------------------
% Note:
% 1. This function should be called in the figure callback WindowButtonMotionFcn.
% 2. It works like GINPUT provided by Matlab,but it traces the position
% of cursor without click and is designed for 2-D axes.
% 3. It can also work even the units of figure and axes are inconsistent,
% or the direction of axes is reversed.
% -------------------------------------------------------------------------

MATLAB release MATLAB 6.5 (R13)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (11)
03 Aug 2015 Chris

Chris (view profile)

I've been trying to get this to work. Following the example below - I used this:

function Main

imagename = 'C:\image.jpg';
img = imread(imagename);
img_gray = rgb2gray(img);

f = figure;
a = axes;
h = image(img_gray);


But I get the following error:

Command Window:

Undefined function or variable 'a'.

Error while evaluating figure WindowButtonMotionFcn

Undefined function or variable 'a'.

Error while evaluating figure WindowButtonMotionFcn

Undefined function or variable 'a'.

Any thoughts?

Comment only
13 Nov 2012 Divakar Roy

Divakar Roy (view profile)

Alisha, I have just uploaded a file on here, which might be what you need. Take a look -

Comment only
22 May 2012 Alisha

Alisha (view profile)

Sorry, I'm not too familiar with MATLAB's GUI capabilities, but is there a way, perhaps modifying Tuss' example, to get the coordinates stored into a variable? I can get the coordinates to update in real time in the command window, but I'd like to take them and plot them on a different plot and I'm not sure how to do that. Thanks

19 Apr 2011 Matías Alonso

This should be native on MATLAB.

22 Aug 2008 Daniel P.

I haven't taken a look at the all code yet and I haven't done any testing yet, but there *might* be a bug: see lines 45 and 46. A variable named "left_origin" is defined in line 45, but it is never used.
In line 46, a variable named "left_origin_unitfig" is used. At this point, the variable still has the value it got in line 42. I don't know if that's intended or not.

Daniel P.

Comment only
28 Mar 2008 Tuss P.

Brilliant m file, Zhao. Thank you very much for sharing this with us. I noticed that your function returns only x coordinates on the axes twice rather than x and y coordinates at the same time. I invoke the function like this.
"f = figure;
a = axes;
h = plot(t,x);
set(f,'windowbuttonmotionfcn','gpos(a)'); "

Should there be anything wrong with this, please kindly let me know. But as far as I have tried, no errors at all.

To Frank, please note the argument when you invoke

It requires string argument rather function handle. You need axes handle and figure handle as shown below.

f = figure;
a = axes;
plot(something you want)

Hope this helps. Please let me know if you have further problems.

29 Jan 2008 Frank Pezzullo

HI ... can i have an example code to run your function ... I try with my example but doesn't work. Thanks

My Example is:

t = (0:0.05:1)';
d1 = sin(2*pi*t)*[1 2 3];
d2 = cos(2*pi*t)*[1 2 3];
h=plot(t,[d1 d2]);

18 Jan 2008 c Zhao

easy to use

08 Mar 2007 Dietrich Lueerssen

The function is very useful (for me at least), and exactly what I wanted.

I discovered something in the code that may be an inadvertent mistake that only is important when the 'Dir' property of the x-axis is not set to 'normal'.

I am including here the original code as well as the fix (it replaces lines 45-46 in the original posting).

% the following lines are the original code obtained from MATLAB
% Central on 8 March 2006
% left_origin = pos_axes_unitfig(1) + width_axes_unitfig;
% x_cursor2origin_unitfig = -( pos_cursor_unitfig(1) - left_origin_unitfig );
% this correction was proposed in order to avoid a possibly not
% initialised (and in addition, an unused) variable
% Dietrich Lueerssen
left_origin_unitfig = pos_axes_unitfig(1) + width_axes_unitfig;
x_cursor2origin_unitfig = -( pos_cursor_unitfig(1) - left_origin_unitfig );

20 Dec 2005 Jasper Menger

Nicely done!

Perhaps needless to say, but the function also works fine when called through a 'DownButtonFcn' callback.

07 May 2004 drew slusser

15 Nov 2008 1.1

bug fix

Contact us