Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: How to set the position of the data cursor programmitcally

Subject: How to set the position of the data cursor programmitcally

From: Anup Mantri

Date: 16 Aug, 2007 13:22:18

Message: 1 of 6

Hi

I am trying to set the position of the data cursor non-interactively.

I have two related images each having a custom data cursor function. I
am trying to link them so if the user changes the position of the data
cursor in one image, the data cursor in the other image is also
updated.

Is there a way to programmitcally send an event to the data cursor
object with the desired coordinates? Or is there a property that I can
change to achieve the same effect?

Thanks
Anup

Subject: How to set the position of the data cursor programmitcally

From: us

Date: 16 Aug, 2007 14:50:35

Message: 2 of 6

Anup Mantri:
<SNIP wants to link his/her data cursors...

one of the solutions is outlined below
- copy the snippet into <linkcur.m>
- at the command prompt, type
     linkcur;
- move your mouse in the UPPER panel

us

function linkcur
% create the plots
     sh(1)=subplot(2,1,1);
     plot(rand(10,1));
     sh(2)=subplot(2,1,2);
     plot(rand(10,1));
% ... and the crosshair pointers
for i=1:2
     xl{i}=get(sh(i),'xlim');
     yl{i}=get(sh(i),'ylim');
     lp{i}=line([xl{i};0,0].',[0,0;yl{i}].',...
               'parent',sh(i),...
               'color',[1,0,0]);
end
     set(gcf,'windowbuttonmotionfcn',{@cb,sh,xl,yl,lp});
     shg;
end
% the callback
function cb(h,e,sh,xl,yl,lp)
     p=get(sh(1),'currentpoint');
for i=1:2
     cp=max([xl{i}(1),p(1,1)]);
     cp=min([xl{i}(2),cp]);
     set(lp{i}(2),'xdata',[cp,cp]);
     cp=max([yl{i}(1),p(1,2)]);
     cp=min([yl{i}(2),cp]);
     set(lp{i}(1),'ydata',[cp,cp]);
end
end

Subject: How to set the position of the data cursor programmitcally

From: Yair Altman

Date: 16 Aug, 2007 17:44:25

Message: 3 of 6

You can use the linkprop function to link your data cursors'
xdata/ydata/zdata properties.

A related info bit (I know this is not what you asked) that
users may find interesting is how to programmatically move
the mouse cursor (pointer). The answer is to
set(0,'PointerLocation',newPosition) or to use the moveptr
function. Note: MathWorks considers moveptr as unsupported -
it's partly documented (it has a help section, but no doc
page), but when I tried reporting some internal bugs and
fixes to MathWorks I got the "undocumented & unsupported"
shrug-off... You need to pass it handle(hAx) where hAx is
your axes handle, and it works well in simple cases
(non-complicated GUIs). moveptr.m is editable and so can
easily be adapted by anyone interested.

Yair Altman
http://ymasoftware.com

Subject: How to set the position of the data cursor programmitcally

From: us

Date: 16 Aug, 2007 18:08:45

Message: 4 of 6

Yair Altman:
<SNIP down to - questionable - advice... in the OP's
context...

> You can use the linkprop function to link your data
cursors' xdata/ydata/zdata properties...

is that what you want?

% let's see what happens
     sh(1)=subplot(2,1,1);
           ph{1}=plot(rand(10,1));
     sh(2)=subplot(2,1,2);
           ph{2}=plot(rand(20,2));
     title('not linked');
     pause;
     title('linked');
     lp=linkprop(cat(1,ph{:}),{'xdata','ydata'});

% also, another way of controlling mous/pointer
% is shown here

% create a roboter
     import java.awt.*;
     import java.awt.event.*;
     rob=Robot;
     ss=get(0,'screensize')
     sy=fix(ss(4)/2)
for i=1:ss(3)
     rob.mouseMove(i,sy);
     pause(.01);
end

% to see more roboter options
     methods(rob);

just a few thoughts
us

Subject: How to set the position of the data cursor programmitcally

From: Anup Mantri

Date: 17 Aug, 2007 20:14:15

Message: 5 of 6

On Aug 16, 2:08 pm, "us " <u...@neurol.unizh.ch> wrote:
> Yair Altman:
> <SNIP down to - questionable - advice... in the OP's
> context...
>
> > You can use the linkprop function to link your data
>
> cursors' xdata/ydata/zdata properties...
>
> is that what you want?
>
> % let's see what happens
> sh(1)=subplot(2,1,1);
> ph{1}=plot(rand(10,1));
> sh(2)=subplot(2,1,2);
> ph{2}=plot(rand(20,2));
> title('not linked');
> pause;
> title('linked');
> lp=linkprop(cat(1,ph{:}),{'xdata','ydata'});
>
> % also, another way of controlling mous/pointer
> % is shown here
>
> % create a roboter
> import java.awt.*;
> import java.awt.event.*;
> rob=Robot;
> ss=get(0,'screensize')
> sy=fix(ss(4)/2)
> for i=1:ss(3)
> rob.mouseMove(i,sy);
> pause(.01);
> end
>
> % to see more roboter options
> methods(rob);
>
> just a few thoughts
> us



Thanks us and Yair for your solutions. While not exactly what I was
looking for, I did learn a couple of new tricks :)

I should have been more specific. I am using the data cursor obtained
by executing 'datacursormode on' command to display the local pixel
info in my image. I have two of those cursors in two windows, and when
the user moves one of them, I want to update the other one too to
point to the same (x,y) coordinate.

Here is what I am trying (execute this in the command window):
figure; hp1 = plot(rand(10,1)); dcm1 = datacursormode;
figure; hp2 = plot(rand(10,1)); dcm2 = datacursormode;

Now if you click in the plot windows, you get a data tip showing the
(x,y) coordinate of the current point. The current coordinates can be
obtained by info_struct = getCursorInfo(dcm1). info_struct.Position
field contains the current position. Is there a way to update the
position of the datacursor dcm2?

Thanks

Subject: How to set the position of the data cursor programmitcally

From: Yair Altman

Date: 18 Aug, 2007 18:55:17

Message: 6 of 6

 Anup Mantri <anup.mantri@gmail.com> wrote in message
<1187381655.019037.265610@57g2000hsv.googlegroups.com>...
> I should have been more specific. I am using the data
cursor obtained
> by executing 'datacursormode on' command to display the
local pixel
> info in my image. I have two of those cursors in two
windows, and when
> the user moves one of them, I want to update the other one
too to
> point to the same (x,y) coordinate.
>
> Here is what I am trying (execute this in the command window):
> figure; hp1 = plot(rand(10,1)); dcm1 = datacursormode;
> figure; hp2 = plot(rand(10,1)); dcm2 = datacursormode;


use the following syntax to use your own data presentation
function instead of Matlab's default:

set(dcm1,'UpdateFcn',{@myDataTipFcn,dcm2}); % similarly for
dcm2

Then, within myDataTipFcn you can update the other data-cursor.

Yair Altman
http://ymasoftware.com

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
datacursormode us 18 Aug, 2007 17:08:03
robot us 17 Aug, 2007 16:24:14
linkprop us 16 Aug, 2007 14:10:24
methods us 16 Aug, 2007 14:10:24
roboter us 16 Aug, 2007 14:10:24
undocumented Yair Altman 16 Aug, 2007 13:45:22
cursor us 16 Aug, 2007 10:55:21
link us 16 Aug, 2007 10:55:21
graphics us 16 Aug, 2007 10:55:21
code us 16 Aug, 2007 10:55:21
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics