No BSD License  

Highlights from
Trace the Position of Cursor without Click

4.85714

4.9 | 7 ratings Rate this file 43 Downloads (last 30 days) File Size: 2.53 KB File ID: #4170

Trace the Position of Cursor without Click

by Kang Zhao

 

18 Nov 2003 (Updated 15 Nov 2008)

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

| Watch this File

File Information
Description

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  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (8)
07 May 2004 drew slusser  
20 Dec 2005 Jasper Menger

Nicely done!

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

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

18 Jan 2008 c Zhao

easy to use

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

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

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.

Regards,
Daniel P.

19 Apr 2011 Matías Alonso

This should be native on MATLAB.

Please login to add a comment or rating.
Updates
15 Nov 2008

bug fix

Tag Activity for this File
Tag Applied By Date/Time
gui tools Kang Zhao 22 Oct 2008 07:09:40
example Kang Zhao 22 Oct 2008 07:09:40
trace Kang Zhao 22 Oct 2008 07:09:40
position Kang Zhao 22 Oct 2008 07:09:40
cursor Kang Zhao 22 Oct 2008 07:09:40
click Kang Zhao 22 Oct 2008 07:09:40
ginput Kang Zhao 22 Oct 2008 07:09:40
cursor Simon 05 Jan 2010 03:55:30
downbuttonfcn Matías Alonso 19 Apr 2011 23:59:31
cursor Matías Alonso 19 Apr 2011 23:59:37
ginput Matías Alonso 19 Apr 2011 23:59:46

Contact us at files@mathworks.com