Here is an example
function main
x = rand(100,1);
y = rand(100,1);
h = plot(x,y,'.r');
set([h gcf],'hittest','off')
set(gca,'buttondownfcn',@func)
function func(hobj,~)
p = get(hobj,'currentpoint');
d = pdist2([x y],p([1 3]));
[~,ix] = min(d);
line(x(ix),y(ix),'linestyle','none','marker','o')
[x(ix),y(ix)]
end
end
Best Answer