MATLAB: Identifying individual points on scatter plot

button down callbackchildrenMATLABscatter

Hello,
I have the following problem: I need to crossplot two vectors (x and y) and am using scatter for it. Then I need to be able to identify the index of the point on which the user clicked. So, in short the code looks like this:
—————————
h=scatter(x,y,S,C); points=get(h,'Children');
for i=1:numel(children)
set(points(i),'HitTest','on','ButtonDownFcn',{'myFunction',i};
end
—————————
I was hoping that the order of children is the same as order of elements in x and y. Looks like it is actually reversed. I could live with this, provided that Matlab guarantees that it will be always this way, i.e. that the relationship between the order of children and the order of x,y elements is fixed now and forever. x and y are not odrered, and searching by their values, which can be obtained from the src argument of the callback, is not very palatable: the vectors may be pretty large.
I'd appreciate any ideas.
Thank you,
Naum Derzhi

Best Answer

I was about to write: See datatip, but found that it is obsolete.
It is replaces by
Data Cursor Text Can Now Be Programmatically Modified
You can now easily customize the text of datatips. The datacursormode function lets you specify the contents and formatting of text displayed by the data cursor tool. When the data cursor tool is active, you can use its context (right-click) menu to edit or specify the text update function that MATLAB executes to display datatips. For more information, see Data Cursor — Displaying Data Values Interactively in the MATLAB Graphics documentation and datacursormode in the MATLAB Function Reference documentation.
Don't know if that is any good in your scenario.
Figure and Axes have the property CurrentPoint
CurrentPoint
two-element vector: [x-coordinate, y-coordinate]
Location of last button click in this figure. MATLAB sets this property to the location of the pointer at the time of the most recent mouse button press. MATLAB updates this property whenever you press the mouse button while the pointer is in the figure window.
All the markers in a scatter-plot is one patch - it seems.
With the axes property CurrentPoint it should be possible. There is no snap AFAIK. There might be problems with points close to each other.
--- @Walter ---
This is my refresh Matlab graphic course :-) I use R2012a and I'm not up to speed with the new stuff that has been added during the last few years. And the old stuff I'm about to forget:-) Thank, you for keeping an eye on my answers.
I was convinced that each point in scatter is a separate object. That was certainly(?) the case once. However, I did the test below, which I think shows that all points are parts of one patch. Scatter did have problems with performance.
load seamount % copy&paste from help

scatter(x,y,5,z) % copy&paste from help
h1 = get( gcf, 'Child');
h2 = get( h1, 'Child' );
h3 = get( h2, 'Child' );
disp( [ 'h1 is a ', get( h1, 'Type' ) ] )
disp( [ 'h2 is a ', get( h2, 'Type' ) ] )
disp( [ 'h3 is a ', get( h3, 'Type' ) ] )
whos h*
h1 is a axes
h2 is a hggroup
h3 is a patch
Name Size Bytes Class Attributes
h1 1x1 8 double
h2 1x1 8 double
h3 1x1 8 double
>> get( h3, {'Xdata','YData'} )
ans =
[294x1 double] [294x1 double]
>> whos x y z
Name Size Bytes Class Attributes
x 294x1 2352 double
y 294x1 2352 double
z 294x1 2352 double