필터 지우기
필터 지우기

Get index of an x, y data point via user selection in a plot.

조회 수: 54 (최근 30일)
Martin Danz
Martin Danz 2022년 4월 7일
답변: Martin Danz 2022년 4월 8일
Hi,
I have two vectors, x and y shown in a scatter plot. Each value is the result of an evaluation and has its own data set.
i. e. x(1) = evalX(DataSet_1), x(2) = evalX(DataSet_2), ... the same for y
i. e. y(1) = evalY(DataSet_1), y(2) = evalY(DataSet_2), ...
Now I want to have a look in the data sets of points of interest. That is, I'd like to klick a data point in the plot figure and want to get the index of the data point.
The only Idea I have is to write a callback function returning the values of a data point (u and v) and plug them in find(x == u). But the data points are double so checking for equality might be not ideal? Also I find this solution not very elegant.
Any Ideas?
  댓글 수: 1
Riccardo Scorretti
Riccardo Scorretti 2022년 4월 8일
Hi. I would suggest to plot all of the points with a single call to plot, define a callback associated with the object created by the function plot (not with the figure), and then selecting the point by looking for the minimal distance between the (x,y) coordinates and the set of points. That should be robust.

댓글을 달려면 로그인하십시오.

답변 (3개)

KSSV
KSSV 2022년 4월 8일
편집: KSSV 2022년 4월 8일
% random points for demo
x = rand(10,1) ;
y = rand(10,1) ;
% Scatter plot
scatter(x,y)
% clicks by the user
[xi,yi] = getpts() ;
% get the index
idx = knnsearch([x y],[xi yi])

Riccardo Scorretti
Riccardo Scorretti 2022년 4월 8일
I would do like that (thanks to KSSV, I didn't know functions scatter and knnsearch):
function demo
x = rand(10,1);
y = rand(10,1);
hndl = scatter(x, y);
hndl.ButtonDownFcn = @myCallback;
function myCallback(src, evt)
disp('.');
xi = evt.IntersectionPoint(1);
yi = evt.IntersectionPoint(2);
idx = knnsearch([x y], [xi yi])
end
end
By programming myCallback as a nested function, it has access to variables x and y. In my opinion, it is better if the callback is invoked only when the user clicks on any of the points (and not anywhere in the figure). Of course, in this way it is mandatory to use a callback, which perhaps you absolutely want to avoid.

Martin Danz
Martin Danz 2022년 4월 8일
Thanks for the suggestions. It seem's I do not have the toolbox required by knnsearch.
However I came up with a workaround using 3D-Plot scatter3, defining the index as z-values and setting the view to 2D. If I now hover the mouse over a data point, z gives me exactly what I want.
Obviously the workaround will not work for 3D Plots, maybe mathworks will implement a similar functionality in future releases?
Example:
N = 13;
x = rand(N, 1);
y = rand(N, 1);
scatter_wIndex(x, y)
xlabel('x')
ylabel('y')
function h = scatter_wIndex(x, y, varargin)
z = 1:length(1:length(x));
scatter3(x, y, z, varargin{:})
view(2)
end

카테고리

Help CenterFile Exchange에서 Specifying Target for Graphics Output에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by