Dan*_*Dan 1 matlab user-interface callback
我希望 Matlab 执行一个函数,该函数将我单击的特定点作为输入,例如,如果我绘制
plot(xy(:,1),xy(:,2))
scatter(xy(:,1),xy(:,2))
然后点击一个特定的点(见图),它会执行一个回调函数,其输入不仅是该点的x,y坐标,还有它的索引值(即变量xy的第4行)
多谢!

ButtonDownFcn这可以通过使用对象的属性来完成Scatter。
在主脚本中:
% --- Define your data
N = 10;
x = rand(N,1);
y = rand(N,1);
% --- Plot and define the callback
h = scatter(x, y, 'bo');
set(h, 'ButtonDownFcn', @myfun);
并在函数中myfun:
function myfun(h, e)
% --- Get coordinates
x = get(h, 'XData');
y = get(h, 'YData');
% --- Get index of the clicked point
[~, i] = min((e.IntersectionPoint(1)-x).^2 + (e.IntersectionPoint(2)-y).^2);
% --- Do something
hold on
switch e.Button
    case 1, col = 'r';
    case 2, col = 'g';
    case 3, col = 'b';
end
plot(x(i), y(i), '.', 'color', col);
i是单击点的索引,因此x(i)和y(i)是单击点的坐标。
令人惊讶的是,执行该操作的鼠标按钮存储在e.Button:
所以你也可以尝试一下。结果如下:

最好的,