File Exchange

image thumbnail

Trace the Position of Cursor without Click

version 1.1 (2.53 KB) by

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

4.875
8 Ratings

1 Download

Updated

No License

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.
% -------------------------------------------------------------------------

Comments and Ratings (11)

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);
set(f,'windowbuttonmotionfcn','gpos(a)');

end

But I get the following error:

Command Window:

>>test
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?

Divakar Roy

Divakar Roy (view profile)

Alisha, I have just uploaded a file on here, which might be what you need. Take a look - http://www.mathworks.com/matlabcentral/fileexchange/38997

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

This should be native on MATLAB.

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.

Regards,
Daniel P.

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
"gpos(h)
set(h,'WindowButtonMotionFcn',@gpos)"

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)
set(f,'windowbuttonmotionfcn','gpos(a)');

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

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]);
gpos(h)
set(h,'WindowButtonMotionFcn',@gpos)

c Zhao

easy to use

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 );

Jasper Menger

Nicely done!

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

drew slusser

Updates

1.1

bug fix

MATLAB Release
MATLAB 6.5 (R13)

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

» Watch video