Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
how to get current object without clicking

Subject: how to get current object without clicking

From: Nick Stokes

Date: 11 May, 2010 17:38:04

Message: 1 of 36

Hi,

Is there a way to detect which object (line, patch, etc..) is at a given cursor position?

Subject: how to get current object without clicking

From: us

Date: 11 May, 2010 19:38:04

Message: 2 of 36

"Nick Stokes" <nickstokes5@gmail.com> wrote in message <hsc4ls$avn$1@fred.mathworks.com>...
> Hi,
>
> Is there a way to detect which object (line, patch, etc..) is at a given cursor position?

show an example...

us

Subject: how to get current object without clicking

From: Steven Lord

Date: 11 May, 2010 21:07:06

Message: 3 of 36


"us " <us@neurol.unizh.ch> wrote in message
news:hscbms$b5q$1@fred.mathworks.com...
> "Nick Stokes" <nickstokes5@gmail.com> wrote in message
> <hsc4ls$avn$1@fred.mathworks.com>...
>> Hi, Is there a way to detect which object (line, patch, etc..) is at a
>> given cursor position?
>
> show an example...

... and explain what you'd like to occur if there is no object at a given
cursor position, or if there are multiple objects at the cursor position.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: how to get current object without clicking

From: Walter Roberson

Date: 11 May, 2010 21:38:59

Message: 4 of 36

Steven Lord wrote:
> "us " <us@neurol.unizh.ch> wrote in message
> news:hscbms$b5q$1@fred.mathworks.com...
>> "Nick Stokes" <nickstokes5@gmail.com> wrote in message
>> <hsc4ls$avn$1@fred.mathworks.com>...
>>> Hi, Is there a way to detect which object (line, patch, etc..) is at a
>>> given cursor position?
>> show an example...
>
> ... and explain what you'd like to occur if there is no object at a given
> cursor position, or if there are multiple objects at the cursor position.

I interpret the question as one of asking, "Is there a way to tell which
object would be selected or activated if one were to click at a given cursor
position?" -- which must be possible to find out in theory because Matlab has
to do the same kind of computation if a click is actually done.

On the other hand, if that _is_ the question, then one starts getting into
issues about objects whose hittest is off, as the top-most visible object is
not necessarily the object that is active at that cursor location. And if one
is working with a 3D axis, then one must figure out which Z coordinate is the
desired one; in particular, one must know whether one wants to intersect
perpendicular to the viewing axis or if one wants to intersect perpendicular
to the screen, or to phrase it differently, whether to treat the cursor
position as 2 dimensional or 3 dimensional.

Subject: how to get current object without clicking

From: Nick Stokes

Date: 11 May, 2010 22:15:22

Message: 5 of 36

Walter Roberson <roberson@hushmail.com> wrote in message > I interpret the question as one of asking, "Is there a way to tell which
> object would be selected or activated if one were to click at a given cursor
> position?" -- which must be possible to find out in theory because Matlab has
> to do the same kind of computation if a click is actually done.
>

yes, that is exactly what I am asking.

> On the other hand, if that _is_ the question, then one starts getting into
> issues about objects whose hittest is off,
> [...]

These are details, not really that pertinent. Let's assume easiest set or whatever matlab assumes when it determines CurrentObject of the figure.

Subject: how to get current object without clicking

From: us

Date: 12 May, 2010 00:54:04

Message: 6 of 36

"Nick Stokes" <nickstokes5@gmail.com> wrote in message <hscktq$n69$1@fred.mathworks.com>...
> Walter Roberson <roberson@hushmail.com> wrote in message > I interpret the question as one of asking, "Is there a way to tell which
> > object would be selected or activated if one were to click at a given cursor
> > position?" -- which must be possible to find out in theory because Matlab has
> > to do the same kind of computation if a click is actually done.
> >
>
> yes, that is exactly what I am asking.
>
> > On the other hand, if that _is_ the question, then one starts getting into
> > issues about objects whose hittest is off,
> > [...]
>
> These are details, not really that pertinent. Let's assume easiest set or whatever matlab assumes when it determines CurrentObject of the figure.

well... it surely uses the position of the last m-click...
i guess it's obvious that CSSMers cannot really follow your problem...
you really should be more specific about what you want...

us

Subject: how to get current object without clicking

From: Nick Stokes

Date: 12 May, 2010 15:21:06

Message: 7 of 36


> i guess it's obvious that CSSMers cannot really follow your problem...

What/who is "CSSMer"?

> you really should be more specific about what you want...
>

The question is a very simple one. But you're right, I should be more specific:

Prototype: H = PIXEL2HANDLE( HF, X, Y )

Given a figure HF, and the pixel coordinates X, Y relative to the lower-left corner of HF, identify the object H such that if there was to be a mouse click on that pixel coordinate, that object would be the "CurrentObject" of the figure HF. If there is no such object, H is empty.

> well... it surely uses the position of the last m-click...

So as you can see (I hope!) that the the crux of the question is we want to be able identify the object *without* the "m-click".

Subject: how to get current object without clicking

From: us

Date: 12 May, 2010 17:52:04

Message: 8 of 36

"Nick Stokes" <nickstokes5@gmail.com> wrote in message <hseh12$6mh$1@fred.mathworks.com>...
>
> > i guess it's obvious that CSSMers cannot really follow your problem...
>
> What/who is "CSSMer"?
>
> > you really should be more specific about what you want...
> >
>
> The question is a very simple one. But you're right, I should be more specific:
>
> Prototype: H = PIXEL2HANDLE( HF, X, Y )
>
> Given a figure HF, and the pixel coordinates X, Y relative to the lower-left corner of HF, identify the object H such that if there was to be a mouse click on that pixel coordinate, that object would be the "CurrentObject" of the figure HF. If there is no such object, H is empty.
>
> > well... it surely uses the position of the last m-click...
>
> So as you can see (I hope!) that the the crux of the question is we want to be able identify the object *without* the "m-click".

firstly, CSSM is this NG: comp.soft-sys.matlab, and a CSSMer is a person romping in it...
then, did you consider a robot(?)...
eg,

http://www.mathworks.com/matlabcentral/newsreader/view_thread/243113#623942

us

Subject: how to get current object without clicking

From: Nick Stokes

Date: 12 May, 2010 20:14:05

Message: 9 of 36

"us " <us@neurol.unizh.ch> wrote in message <hseps4$4sc$1@fred.mathworks.com>...

> > So as you can see (I hope!) that the the crux of the question is we want to be able identify the object *without* the "m-click".
>
> firstly, CSSM is this NG: comp.soft-sys.matlab, and a CSSMer is a person romping in it...
> then, did you consider a robot(?)...
> eg,
>
> http://www.mathworks.com/matlabcentral/newsreader/view_thread/243113#623942
>

thanks, this is interesting stuff. But seriously, that is not the answer -- and probably will not work optimally in general, e.g. in my case, which is to create an interactive plot interface via hovering over the figure sort-of-thing. -- We don't want to click or simulate a click. Actually we don't even want to update the CurrentObject of the figure.

Subject: how to get current object without clicking

From: someone

Date: 12 May, 2010 20:27:21

Message: 10 of 36

"Nick Stokes" <nickstokes5@gmail.com> wrote in message <hsf26d$6be$1@fred.mathworks.com>...
> "us " <us@neurol.unizh.ch> wrote in message <hseps4$4sc$1@fred.mathworks.com>...
>
> > > So as you can see (I hope!) that the the crux of the question is we want to be able identify the object *without* the "m-click".
> >
> > firstly, CSSM is this NG: comp.soft-sys.matlab, and a CSSMer is a person romping in it...
> > then, did you consider a robot(?)...
> > eg,
> >
> > http://www.mathworks.com/matlabcentral/newsreader/view_thread/243113#623942
> >
>
> thanks, this is interesting stuff. But seriously, that is not the answer -- and probably will not work optimally in general, e.g. in my case, which is to create an interactive plot interface via hovering over the figure sort-of-thing. -- We don't want to click or simulate a click. Actually we don't even want to update the CurrentObject of the figure.

And I want world peace.

Subject: how to get current object without clicking

From: Nick Stokes

Date: 14 May, 2010 15:30:22

Message: 11 of 36

"someone" <someone@somewhere.net> wrote in message <hsf2v9$r3v$1@fred.mathworks.com>...

> >
> > thanks, this is interesting stuff. But seriously, that is not the answer -- and probably will not work optimally in general, e.g. in my case, which is to create an interactive plot interface via hovering over the figure sort-of-thing. -- We don't want to click or simulate a click. Actually we don't even want to update the CurrentObject of the figure.
>
> And I want world peace.

I am having hard time understanding why this is creating such sarcastic awe. As Walter was saying earlier, if MATLAB can determine what object is being clicked, there must be a lower-level functionality that it might be using in doing this.

Fwiw, let me show an example of an interactive visualization where such functionality would be essential:

http://www.nytimes.com/interactive/2008/10/11/business/20081011_BEAR_MARKETS.html

Subject: how to get current object without clicking

From: Doug Schwarz

Date: 14 May, 2010 15:53:00

Message: 12 of 36

In article <hsjqae$eg$1@fred.mathworks.com>,
 "Nick Stokes" <nickstokes5@gmail.com> wrote:

> "someone" <someone@somewhere.net> wrote in message
> <hsf2v9$r3v$1@fred.mathworks.com>...
>
> > >
> > > thanks, this is interesting stuff. But seriously, that is not the answer
> > > -- and probably will not work optimally in general, e.g. in my case,
> > > which is to create an interactive plot interface via hovering over the
> > > figure sort-of-thing. -- We don't want to click or simulate a click.
> > > Actually we don't even want to update the CurrentObject of the figure.
> >
> > And I want world peace.
>
> I am having hard time understanding why this is creating such sarcastic awe.
> As Walter was saying earlier, if MATLAB can determine what object is being
> clicked, there must be a lower-level functionality that it might be using in
> doing this.
>
> Fwiw, let me show an example of an interactive visualization where such
> functionality would be essential:
>
> http://www.nytimes.com/interactive/2008/10/11/business/20081011_BEAR_MARKETS.h
> tml


Perhaps you can modify overobj.m to do what you want.

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Subject: how to get current object without clicking

From: us

Date: 14 May, 2010 16:43:04

Message: 13 of 36

"Nick Stokes" <nickstokes5@gmail.com> wrote in message <hsjqae$eg$1@fred.mathworks.com>...
> "someone" <someone@somewhere.net> wrote in message <hsf2v9$r3v$1@fred.mathworks.com>...
>
> > >
> > > thanks, this is interesting stuff. But seriously, that is not the answer -- and probably will not work optimally in general, e.g. in my case, which is to create an interactive plot interface via hovering over the figure sort-of-thing. -- We don't want to click or simulate a click. Actually we don't even want to update the CurrentObject of the figure.
> >
> > And I want world peace.
>
> I am having hard time understanding why this is creating such sarcastic awe. As Walter was saying earlier, if MATLAB can determine what object is being clicked, there must be a lower-level functionality that it might be using in doing this.
>
> Fwiw, let me show an example of an interactive visualization where such functionality would be essential:
>
> http://www.nytimes.com/interactive/2008/10/11/business/20081011_BEAR_MARKETS.html

well...
firstly, some replies have NOT been sarcastic at all...
second, NOW things seem more clear as you explain them in better MLbbers terms, because the way you first phrased it, CSSMers got the impression that you don't want to use the mouse AT ALL...
to achieve the above demonstrated functionality, look at this callbacks

     set(gcf,'windowbuttonmotionfcn',@your_callback);
% eg,
     plot(1:10);
     fh=@(varargin) disp(sprintf('%d %d',get(gcf,'currentpoint')));
     set(gcf,'windowbuttonmotionfcn',fh);
% now, move your mouse and look at the command window...
% your callback would be more intelligent, of course...

us

Subject: how to get current object without clicking

From: Jan Simon

Date: 14 May, 2010 16:49:04

Message: 14 of 36

Dear Doug!

> Perhaps you can modify overobj.m to do what you want.

How can I get the screen coordinates for docked figures?

Jan

Subject: how to get current object without clicking

From: us

Date: 14 May, 2010 17:08:05

Message: 15 of 36

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <hsjuu0$4o3$1@fred.mathworks.com>...
> Dear Doug!
>
> > Perhaps you can modify overobj.m to do what you want.
>
> How can I get the screen coordinates for docked figures?
>
> Jan

jan
my snippet (see above) works for docked figs as well...

urs

Subject: how to get current object without clicking

From: Jan Simon

Date: 14 May, 2010 18:58:06

Message: 16 of 36

Dear Urs!

> > Dear Doug!
> > How can I get the screen coordinates for docked figures?
> > Jan
 
> my snippet (see above) works for docked figs as well...
> urs

Yes, but Doug's hint to OVEROBJ calls:
  p = get(0, 'PointerLocation');
  figPos = get(fig, 'Position');
This fails for a docked figure, doesn't it?
Are there any workarounds?

Jan

Subject: how to get current object without clicking

From: us

Date: 14 May, 2010 19:11:19

Message: 17 of 36

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <hsk6ft$ojr$1@fred.mathworks.com>...
> Dear Urs!
>
> > > Dear Doug!
> > > How can I get the screen coordinates for docked figures?
> > > Jan
>
> > my snippet (see above) works for docked figs as well...
> > urs
>
> Yes, but Doug's hint to OVEROBJ calls:
> p = get(0, 'PointerLocation');
> figPos = get(fig, 'Position');
> This fails for a docked figure, doesn't it?
> Are there any workarounds?
>
> Jan

hmm... but the (undoc) OVEROBJ works in docked figs as well, at least in r2010a...

     fh=@(varargin) disp(sprintf('%g',overobj('axes')));
     plot(1:10);
     set(gcf,'windowbuttonmotionfcn',fh);
% hover
% dock the fig
% hover, again...

most likely, though, i don't understand the problem...
urs

Subject: how to get current object without clicking

From: Jan Simon

Date: 14 May, 2010 19:33:06

Message: 18 of 36

Dear Urs!

> hmm... but the (undoc) OVEROBJ works in docked figs as well, at least in r2010a...
> fh=@(varargin) disp(sprintf('%g',overobj('axes')));
> plot(1:10);
> set(gcf,'windowbuttonmotionfcn',fh);
> % hover
> % dock the fig
> % hover, again...
>
> most likely, though, i don't understand the problem...

The problem is easy to describe: Matlab 2009a.
There OVEROBJ fails for docked figures: The correct object cannot be found, because the position of docked figures in pixels is [1, 1, Width, Height] ever and the absolute screen position cannot be determined.

Jan

Subject: how to get current object without clicking

From: Nick Stokes

Date: 14 May, 2010 22:17:04

Message: 19 of 36

@us

> well...
> firstly, some replies have NOT been sarcastic at all...
> second, NOW things seem more clear as you explain them in better MLbbers terms,
> because the way you first phrased it, CSSMers got the impression that you don't
> want to use the mouse AT ALL...

I still don't understand why and how come you can talk for everyone else in the list with such certainty, but anyhow...

> to achieve the above demonstrated functionality, look at this callbacks
> set(gcf,'windowbuttonmotionfcn',@your_callback);
> % eg,
> plot(1:10);
> fh=@(varargin) disp(sprintf('%d %d',get(gcf,'currentpoint')));
> set(gcf,'windowbuttonmotionfcn',fh);
> % now, move your mouse and look at the command window...

This is useless!! None of this code tells anything about what OBJECT is under the currentpoint! Of course we'd be implementing 'windowbuttonmotionfcn' at some point for the interactive example I'd posted, but that is totally irrelevant.

> % your callback would be more intelligent, of course...

yes! and I tried to spare you all this when I was asking the question to begin with. Now hopefully that we are on the same page, and now that we have the cursor position via get(gcf,'currentpoint') in some kind of callback or whatever, let's move on:

How do I determine which object is at that point? And, although interesting thought (and thanks for posting earlier), we do not want to call the "robot" to do the clicking because (1) it is not elegant (2) we don't want to update figure's currentobject (3) it doesn't answer the question.

(and please, please, do not answer if you don't know the exact answer to this. spare me the irrelevant)

@ "Doug Schwarz'

> Perhaps you can modify overobj.m to do what you want.

This was a good lead! Thank you. But it seems to work only for objects that are immediate children of a figure. In particular, it is a simplistic code that checks for bounding box of the objects, and determines whether current point is underneath. Probably very useful for UI, but not general line, patch, etc.. (no bounding box trick will work there).

Subject: how to get current object without clicking

From: dpb

Date: 14 May, 2010 22:28:44

Message: 20 of 36

Nick Stokes wrote:
...

> (and please, please, do not answer if you don't know the exact answer
> to this. spare me the irrelevant)
...

OK, I'll do so instead... :)

Have you considered it just simply may not be possible to a user app in
Matlab and that ML internals are available to ML itself in an obviously
much more intimate manner than at the m-file level?

--

Subject: how to get current object without clicking

From: Nick Stokes

Date: 14 May, 2010 22:44:05

Message: 21 of 36

dpb <none@non.net> wrote in message <hskiu8$d9b$2@news.eternal-september.org>...

>
> OK, I'll do so instead... :)
>
> Have you considered it just simply may not be possible to a user app in
> Matlab

Yes, thanks! That would be an answer indeed.. Unfortunate, but an answer nevertheless.

> and that ML internals are available to ML itself in an obviously
> much more intimate manner than at the m-file level?
>

But is that /really/ the case? Isn't there even an undocumented, unsupported, low-level, platform-specific way?

Subject: how to get current object without clicking

From: dpb

Date: 14 May, 2010 22:59:52

Message: 22 of 36

Nick Stokes wrote:
> dpb <none@non.net> wrote in message
> <hskiu8$d9b$2@news.eternal-september.org>...
>
>>
>> OK, I'll do so instead... :)
>>
>> Have you considered it just simply may not be possible to a user app
>> in Matlab
>
> Yes, thanks! That would be an answer indeed.. Unfortunate, but an
> answer nevertheless.
>> and that ML internals are available to ML itself in an obviously much
>> more intimate manner than at the m-file level?
>>
>
> But is that /really/ the case? Isn't there even an undocumented,
> unsupported, low-level, platform-specific way?

I have no idea (I don't do windows/gui's even if had a current version
of Matlab to futz with so anything that did would have any bearing
whatsoever).

But, it strikes me that even if you were to find such a back door you
would have invested a great deal of time and effort into an undocumented
hack that could change behavior at any release.

I have no idea what your target is here but to invest what appears to be
a significant amount of effort for such an undocumented feature seems a
risky business model for anything other than simply for speculation to
prove a point.

--

Subject: how to get current object without clicking

From: us

Date: 15 May, 2010 00:58:04

Message: 23 of 36

"Nick Stokes" <nickstokes5@gmail.com> wrote in message <hski50$71p$1@fred.mathworks.com>...
> @us
>
> > well...
> > firstly, some replies have NOT been sarcastic at all...
> > second, NOW things seem more clear as you explain them in better MLbbers terms,
> > because the way you first phrased it, CSSMers got the impression that you don't
> > want to use the mouse AT ALL...
>
> I still don't understand why and how come you can talk for everyone else in the list with such certainty, but anyhow...
>
> > to achieve the above demonstrated functionality, look at this callbacks
> > set(gcf,'windowbuttonmotionfcn',@your_callback);
> > % eg,
> > plot(1:10);
> > fh=@(varargin) disp(sprintf('%d %d',get(gcf,'currentpoint')));
> > set(gcf,'windowbuttonmotionfcn',fh);
> > % now, move your mouse and look at the command window...
>
> This is useless!! None of this code tells anything about what OBJECT is under the currentpoint! Of course we'd be implementing 'windowbuttonmotionfcn' at some point for the interactive example I'd posted, but that is totally irrelevant.
>
> > % your callback would be more intelligent, of course...
>
> yes! and I tried to spare you all this when I was asking the question to begin with. Now hopefully that we are on the same page, and now that we have the cursor position via get(gcf,'currentpoint') in some kind of callback or whatever, let's move on:
>
> How do I determine which object is at that point? And, although interesting thought (and thanks for posting earlier), we do not want to call the "robot" to do the clicking because (1) it is not elegant (2) we don't want to update figure's currentobject (3) it doesn't answer the question.
>
> (and please, please, do not answer if you don't know the exact answer to this. spare me the irrelevant)

there is no need for you to get brash and flippant at all, and i will show you why

*** 05/11: you start this thread with a simple one liner
...Is there a way to detect which object (line, patch, etc..) is at a given cursor position?...

*** 05/11, exactly two hours later: i respond with a simple one liner
show an example...

for three days, several excellent (certainly not me) people have tried to find out how to help you...

however, it takes you four (4) fuzzy replies until - finally - on
*** 05/14: you bother to give the example you could have given in the OP
...Fwiw, let me show an example of an interactive visualization where such functionality would be essential:...

note: you use the term 'FWIW', which is pretty 'cool' given the fact that some people spent their time to find out what your particular problem is...

given your track record of one OP in this NG (i realize, it's just this particular email address) you sound a bit too condescending for what you deliver yourself - a behaviour not boding well for your future here at CSSM...

us

Subject: how to get current object without clicking

From: Nick Stokes

Date: 15 May, 2010 03:38:03

Message: 24 of 36

"us " <us@neurol.unizh.ch> wrote in message

> for three days, several excellent (certainly not me) people have tried to find out how to help you...

yes, I am indeed very grateful (even for your efforts). no sarcasm, really.

> however, it takes you four (4) fuzzy replies until - finally - on [...]

ok (patience ...). I still don't understand why you're going about tagging newcomers messages as "scolding". low blow at its best). But I respect you taking time to read my questions.

Please let's sum it up and converge. Look:

> >
> > > to achieve the above demonstrated functionality, look at this callbacks
> > > set(gcf,'windowbuttonmotionfcn',@your_callback);
> > > % eg,
> > > plot(1:10);
> > > fh=@(varargin) disp(sprintf('%d %d',get(gcf,'currentpoint')));
> > > set(gcf,'windowbuttonmotionfcn',fh);
> > > % now, move your mouse and look at the command window...
> >
> > This is useless!! None of this code tells anything about what OBJECT is under the currentpoint! Of course we'd be implementing 'windowbuttonmotionfcn' at some point for the interactive example I'd posted, but that is totally irrelevant.

do you agree that the above snippet you posted do not answer the question? Seriously, I am not trying to offend you or anything (how one can offend anyone one doesn't know, I don't know). It is just plain, civil, logical discussion here.

I didn't post an example at first thinking it wasn't relevant. But I concede, I was wrong. Now in summary we have:

*The title:* how to get current object without clicking
*The one-line (succinct) question:* .. detect which object (line, patch, etc..) is at a given cursor position?
*The elaboration:* H = PIXEL2HANDLE( HF, X, Y )
*The example:* http://tinyurl.com/47tnhl

All thanks to you all, finally I am able to ask the question right. Thank you for all the efforts (again seriously, no sarcarm).

Now instead of begin "scolded", maybe this the-single-OP-one-liner-NG-disabled-devoid-of-good-CSSM-future-poor-soul Nick can hope for an answer? (sry, couldn't help the sarcasm on this one, but only hoping this would lead to a chuckle this once instead of asperity uncalled for)

NB: this news thing is hard.

- nick

Subject: how to get current object without clicking

From: Nick Stokes

Date: 15 May, 2010 04:04:18

Message: 25 of 36

dpb <none@non.net> wrote in message <hskkok$t9c$1@news.eternal-september.org>...
> [..]
>
> But, it strikes me that even if you were to find such a back door you
> would have invested a great deal of time and effort into an undocumented
> hack that could change behavior at any release.
>
> I have no idea what your target is here but to invest what appears to be
> a significant amount of effort for such an undocumented feature seems a
> risky business model for anything other than simply for speculation to
> prove a point.
>

I am not trying to implement a general-use visualization tool as in the JS example I posted earlier (http://tinyurl.com/47tnhl), but a small useful utility. It won't be nearly as much work as it appears given such a function exists. Indeed, it will be a few liner the general idea of which is as follows:

plot( a_lot_of_lines );
set(gcf, 'windowbuttonmotionfcn', @foo );
function foo(s,e)
   persistent hPrev
   x = get(s,'currentpoint');
   h = pixel2handle (s, x); % the magic
   if isempty( h ) return;
   stand( h ); % annotate, bring forward, or change color, or something
   if ~isempty(hPrev) sit( hPrev ); % revert original style
   hPrev = h;
end

This would be simple tool I'd love to use to sift through crossing lines and messy plots.
 

Subject: how to get current object without clicking

From: Jan Simon

Date: 15 May, 2010 13:55:10

Message: 26 of 36

Dear Nick!

> plot( a_lot_of_lines );
> set(gcf, 'windowbuttonmotionfcn', @foo );
> function foo(s,e)
> persistent hPrev
> x = get(s,'currentpoint');
> h = pixel2handle (s, x); % the magic
> if isempty( h ) return;
> stand( h ); % annotate, bring forward, or change color, or something
> if ~isempty(hPrev) sit( hPrev ); % revert original style
> hPrev = h;
> end

If you want PIXEL2HANDLE to be simple, use the robot to simulate a click and let the callback function of the objects solve the rest.
If you do not want to change the figure's current object (and you have any reason for that), then let the robot restore the former current object afterwards.

Or specify which objects hould be affected by the operation - Pixel2LineHandle should be much simpler. There must be some example code in the measurement utilities of Matlab, which find the nearest line inside a certain distance, when the mouse is clicked.

Or you can use a simple mechanism which uses left-&right mouse clicks instead of hover&left click. It is not worth to spend a lot of time to develop a GUI which differs from the expectations and experiences of standard users.

Or if you are really fond of your Java example, why don't you use it and omit the translation to Matlab?

Or you can give each object an individual color and recognize the object by using the Java robot again: get the color of the current point... Ugly, I know.

Good luck, Jan

Subject: how to get current object without clicking

From: Yair Altman

Date: 15 May, 2010 21:47:06

Message: 27 of 36

> to achieve the above demonstrated functionality, look at this callbacks
> set(gcf,'windowbuttonmotionfcn',@your_callback);
> % eg,
> plot(1:10);
> fh=@(varargin) disp(sprintf('%d %d',get(gcf,'currentpoint')));
> set(gcf,'windowbuttonmotionfcn',fh);
> % now, move your mouse and look at the command window...
[...]
> @ "Doug Schwarz'
>
> > Perhaps you can modify overobj.m to do what you want.
>
> This was a good lead! Thank you. But it seems to work only for objects that are immediate children of a figure. In particular, it is a simplistic code that checks for bounding box of the objects, and determines whether current point is underneath. Probably very useful for UI, but not general line, patch, etc.. (no bounding box trick will work there).

Aside from overobj being undocumented, it also has some limitations, one of which you mentioned (it only searches direct figure children, not inner descendants, so anything contained within a uipanel, uibuttongroup etc. cannot be found).

Another limitation is that it uses the findobj function which is relatively slow and so it is not recommended to use overobj in a complex figure’s WindowButtonMotionFcn callback (http://www.mathworks.com/matlabcentral/newsreader/view_thread/27539#68650 ).

Also, it assumes the root and figure units are both ‘pixel’: often they are not. Also, it only searches visible objects: hidden axes are not retrieved. Finally, it would be a great benefit to have an overobj variant in which the Type argument is optional, so the function would return the handle of the first object found, of whichever type.

So, I have written a variant of overobj that fixes all these issues, except the slowness. Note that overobj and overobj2 only work on objects having a Position property, and so cannot be used for axes plot children:

function h = overobj2(varargin)
%OVEROBJ2 Get handle of object that the pointer is over.
% H = OVEROBJ2 searches all objects in the PointerWindow
% looking for one that is under the pointer. Returns first
% object handle it finds under the pointer, or empty matrix.
%
% H = OVEROBJ2(FINDOBJ_PROPS) searches all objects which are
% descendants of the figure beneath the pointer and that are
% returned by FINDOBJ with the specified arguments.
%
% Example:
% h = overobj2('type','axes');
% h = overobj2('flat','visible','on');
%
% See also OVEROBJ, FINDOBJ

% Ensure root units are pixels
oldUnits = get(0,'units');
set(0,'units','pixels');

% Get the figure beneath the mouse pointer, and mouse pointer pos
fig = get(0,'PointerWindow');
p = get(0,'PointerLocation');
set(0,'units',oldUnits);

% Look for quick exit (if mouse pointer is not over any figure)
if fig==0, h=[]; return; end

% Compute figure offset of mouse pointer in pixels
figPos = getpixelposition(fig);
x = (p(1)-figPos(1));
y = (p(2)-figPos(2));

% Loop over all figure descendants
c = findobj(get(fig,'Children'),varargin{:});
for h = c',
   % If descendant contains the mouse pointer position, exit
   r = getpixelposition(h);
   if ( (x>r(1)) && (x<r(1)+r(3)) && (y>r(2)) && (y<r(2)+r(4)) )
      return
   end
end
h = [];


An alternative to overobj or overobj2 is to use the undocumented hittest built-in function – separate cases may dictate preferring one alternative to the other:
   hObj = hittest(hFig);

Yair Altman
http://UndocumentedMatlab.com

Subject: how to get current object without clicking

From: Yair Altman

Date: 16 May, 2010 05:29:04

Message: 28 of 36

"Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote:

[snip]
> % Loop over all figure descendants
> c = findobj(get(fig,'Children'),varargin{:});
[snip]

Correction: use findall() rather than findobj() above

> An alternative to overobj or overobj2 is to use the undocumented hittest built-in function – separate cases may dictate preferring one alternative to the other:
> hObj = hittest(hFig);

Using hittest() may still be the fastest and best approach.

Yair Altman
http://UndocumentedMatlab.com

Subject: how to get current object without clicking

From: Nick Stokes

Date: 16 May, 2010 23:17:03

Message: 29 of 36



@"Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message <hsnvr0$nje$1@fred.mathworks.com>...
> "Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote:
>
> [..]
>
> > An alternative to overobj or overobj2 is to use the undocumented hittest built-in function – separate cases may dictate preferring one alternative to the other:
> > hObj = hittest(hFig);
>
> Using hittest() may still be the fastest and best approach.
>

thanks Yair. This is definitely improved overobj, yet again, bounding box trick will not work on lines, patches, etc.. so it is not helpful unfortunately.


@ myself @ us
>> "us " <us@neurol.unizh.ch> wrote in message
>> for three days, several excellent (certainly not me) people have tried to find out
>> however, it takes you four (4) fuzzy replies until - finally - on [...]

> ok (patience ...). I still don't understand why you're going about tagging newcomers messages as "scolding". low blow at its best). But I respect you taking time to read my questions.

hey Us, you know what? never mind my remark on that "scolding" thing. Actually thanks to you tagging it I had fun: I was able to browse others, see quite a few people have gotten "scolded" by no other than Mr. Us here (footnote: I'd bet my car that you're indeed a "Mr"). And all this scolding is with recurring remarks: "well...\n", "... cssmers this, mlabber that...", "bla bla is not boding well for your successful future.." :)) lolzies!! Tell you what, your going about scolding people right or wrong and tagging their posts isn't boding well for your successful past.

I mean... I really could have done without all this patronizing given most useful suggestion was one of yours (see below). Thanks for everyone who is trying to sift through this crap and try and answer.

@ Jan Simon
> If you want PIXEL2HANDLE to be simple, use the robot to simulate a click and let the callback function of the objects solve the rest.

Thanks! This was worth a try and I did. To my surprise, it was actually really good! I posted a sample if anyone wants to give it a try:

http://code-bin.homedns.org/649

This is certainly good enough although, the question proper isn't answered (other than someone pointing out the possibility that there may not be a user level routine at all).

thx all - nick

Subject: how to get current object without clicking

From: us

Date: 17 May, 2010 00:26:04

Message: 30 of 36

"Nick Stokes" <nickstokes5@gmail.com> wrote in message <hspude$oev$1@fred.mathworks.com>...
>
>
> @"Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message <hsnvr0$nje$1@fred.mathworks.com>...
> > "Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote:
> >
> > [..]
> >
> > > An alternative to overobj or overobj2 is to use the undocumented hittest built-in function – separate cases may dictate preferring one alternative to the other:
> > > hObj = hittest(hFig);
> >
> > Using hittest() may still be the fastest and best approach.
> >
>
> thanks Yair. This is definitely improved overobj, yet again, bounding box trick will not work on lines, patches, etc.. so it is not helpful unfortunately.
>
>
> @ myself @ us
> >> "us " <us@neurol.unizh.ch> wrote in message
> >> for three days, several excellent (certainly not me) people have tried to find out
> >> however, it takes you four (4) fuzzy replies until - finally - on [...]
>
> > ok (patience ...). I still don't understand why you're going about tagging newcomers messages as "scolding". low blow at its best). But I respect you taking time to read my questions.
>
> hey Us, you know what? never mind my remark on that "scolding" thing. Actually thanks to you tagging it I had fun: I was able to browse others, see quite a few people have gotten "scolded" by no other than Mr. Us here (footnote: I'd bet my car that you're indeed a "Mr"). And all this scolding is with recurring remarks: "well...\n", "... cssmers this, mlabber that...", "bla bla is not boding well for your successful future.." :)) lolzies!! Tell you what, your going about scolding people right or wrong and tagging their posts isn't boding well for your successful past.
>
> I mean... I really could have done without all this patronizing given most useful suggestion was one of yours (see below). Thanks for everyone who is trying to sift through this crap and try and answer.
>
> @ Jan Simon
> > If you want PIXEL2HANDLE to be simple, use the robot to simulate a click and let the callback function of the objects solve the rest.
>
> Thanks! This was worth a try and I did. To my surprise, it was actually really good! I posted a sample if anyone wants to give it a try:
>
> http://code-bin.homedns.org/649
>
> This is certainly good enough although, the question proper isn't answered (other than someone pointing out the possibility that there may not be a user level routine at all).
>
> thx all - nick

a nice - and certainly humorous - wrap-up of an almost never-ending story...
we'll be watching you and hope you'll make it to CSSM soon, again...

:-) to LOL...
us
ps: a male-ish gender, indeed...

Subject: how to get current object without clicking

From: Yair Altman

Date: 17 May, 2010 07:17:08

Message: 31 of 36

"Nick Stokes" <nickstokes5@gmail.com> wrote in message

> Thanks! This was worth a try and I did. To my surprise, it was actually really good! I posted a sample if anyone wants to give it a try:
>
> http://code-bin.homedns.org/649


Why not post it to the Matlab File Exchange? http://www.mathworks.com/matlabcentral/fileexchange/ - it could be an interesting addition.

Yair Altman
http://UndocumentedMatlab.com

p.s. - you have used a setbehavior() function in your code. You're probably not aware that this is a built-in undocumented function that is used for other purposes by internal Matlab code, and might interfere with that code executing if you override the function as you did. Therefore, you may wish to change this function's name in your code

Subject: how to get current object without clicking

From: Nick Stokes

Date: 17 May, 2010 14:32:05

Message: 32 of 36

"Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message <hsqqhk$3qm$1@fred.mathworks.com>...
> "Nick Stokes" <nickstokes5@gmail.com> wrote in message
>
> > Thanks! This was worth a try and I did. To my surprise, it was actually really good! I posted a sample if anyone wants to give it a try:
> >
> > http://code-bin.homedns.org/649
>
>
> Why not post it to the Matlab File Exchange? http://www.mathworks.com/matlabcentral/fileexchange/ - it could be an interesting addition.

Actually I did that initially; but go the notice "it will take 5 days to approve" or something like that. I still can't even see the file any longer in my submission list.

>
> p.s. - you have used a setbehavior() function in your code. You're probably not aware that this is a built-in undocumented function that is used for other purposes by internal Matlab code, and might interfere with that code executing if you override the function as you did. Therefore, you may wish to change this function's name in your code

Yes I wasn't aware, and am still not: I am on 7.9.0 and `which setbehavior` gives not found. Could it be part of a package? anyhow..

Subject: how to get current object without clicking

From: Steven Lord

Date: 17 May, 2010 17:32:10

Message: 33 of 36


"Nick Stokes" <nickstokes5@gmail.com> wrote in message
news:hskjnl$hkp$1@fred.mathworks.com...
> dpb <none@non.net> wrote in message
> <hskiu8$d9b$2@news.eternal-september.org>...
>
>>
>> OK, I'll do so instead... :)
>>
>> Have you considered it just simply may not be possible to a user app in
>> Matlab
>
> Yes, thanks! That would be an answer indeed.. Unfortunate, but an answer
> nevertheless.
>> and that ML internals are available to ML itself in an obviously much
>> more intimate manner than at the m-file level?
>>
>
> But is that /really/ the case? Isn't there even an undocumented,
> unsupported, low-level, platform-specific way?

Doug pointed you to OVEROBJ, which is an undocumented and unsupported way to
do something close to what you want. You're welcome to make a copy of
overobj.m and tweak it to suit your needs better.

I'd also like to suggest that perhaps you should contact Technical Support
and explain specifically what you're trying to do that requires this
functionality, so that they can tell you if there's a way to do what you
want (I'm not sure if there is or is not) and if not they can capture it as
an enhancement request (with your use case attached as motivation.) You can
contact Technical Support using the Contact Us link at the top of
http://www.mathworks.com.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: how to get current object without clicking

From: us

Date: 17 May, 2010 17:49:04

Message: 34 of 36

"Nick Stokes"
> Thanks! This was worth a try and I did. To my surprise, it was actually really good! I posted a sample if anyone wants to give it a try:
>
> http://code-bin.homedns.org/649

well... i see you used the robot-idea after all...

us

Subject: how to get current object without clicking

From: Yair Altman

Date: 17 May, 2010 20:07:04

Message: 35 of 36

"Nick Stokes" <nickstokes5@gmail.com> wrote in message <hsrk15$qsl$1@fred.mathworks.com>...
> "Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message <hsqqhk$3qm$1@fred.mathworks.com>...
> > "Nick Stokes" <nickstokes5@gmail.com> wrote in message
> >
> > > Thanks! This was worth a try and I did. To my surprise, it was actually really good! I posted a sample if anyone wants to give it a try:
> > >
> > > http://code-bin.homedns.org/649
> >
> >
> > Why not post it to the Matlab File Exchange? http://www.mathworks.com/matlabcentral/fileexchange/ - it could be an interesting addition.
>
> Actually I did that initially; but go the notice "it will take 5 days to approve" or something like that. I still can't even see the file any longer in my submission list.

Patience, patience... it's live now:
http://www.mathworks.com/matlabcentral/fileexchange/27632

> > p.s. - you have used a setbehavior() function in your code. You're probably not aware that this is a built-in undocumented function that is used for other purposes by internal Matlab code, and might interfere with that code executing if you override the function as you did. Therefore, you may wish to change this function's name in your code
>
> Yes I wasn't aware, and am still not: I am on 7.9.0 and `which setbehavior` gives not found. Could it be part of a package? anyhow..

You're right - my bad here. 'Behavior' is an undocumented hidden HG property that is used internally for different things: Pan, Zoom, 3D Rotation, Data-Cursor mode and Plot-Editing. However, Behavior is *not( accessed by setbehavior() as I incorrectly remembered, but by hasbehavior(), hgaddbehavior(), hggetbehavior(), and hgbehaviorfactory(). Maybe I'll post about Matlab handle behavior sometime.

Yair Altman
http://UndocumentedMatlab.com

Subject: how to get current object without clicking

From: Nick Stokes

Date: 17 May, 2010 21:51:04

Message: 36 of 36

"Steven Lord" <slord@mathworks.com> wrote in message <hsruim$l7i$1@fred.mathworks.com>...
> >>
> >
> > But is that /really/ the case? Isn't there even an undocumented,
> > unsupported, low-level, platform-specific way?
>
> Doug pointed you to OVEROBJ, which is an undocumented and unsupported way to
> do something close to what you want. You're welcome to make a copy of
> overobj.m and tweak it to suit your needs better.

No Steve. I don't know maybe you've got some delay on these messages; but again (for the 3rd time) overobj is simplistic bounding-box detection routine, and it will not work for lines, or general concave objects.

>
> I'd also like to suggest that perhaps you should contact Technical Support
> and explain specifically what you're trying to do that requires this
> functionality, so that they can tell you if there's a way to do what you
> want (I'm not sure if there is or is not) and if not they can capture it as
> an enhancement request (with your use case attached as motivation.) You can
> contact Technical Support using the Contact Us link at the top of
> http://www.mathworks.com.
>

thanks for the suggestion.

Tags for this Thread

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.

Contact us