How to select point or lines on the graph

조회 수: 5 (최근 30일)
Volkan Kandemir
Volkan Kandemir 2013년 2월 17일
Hi everyone. Please run the following code. It will create a scatter grap with 5 points. I want to select the nearest point on the screen that user click. In other words user can select any point by clicking the mause. It is easy for 5 point but there must be more compact way if there is too many points like 10^4. Do you have any sugestion to increase the speed of this code. I think there must be a beter solution since on the figure menu bar as you all know there is mause icon called "edit plot". User can easly select any data by using it or data curser.
function asd
global points
points=[7 5 9 6 4; 7 4 2 5 8];
ax1=axes('Position',[0 0 1 1]);
scatter(points(1,:),points(2,:));
set(ax1,'ButtonDownFcn',@mauseclick);
grid on
function mauseclick(source, event)
global points
CPoint=get(source,'CurrentPoint')
[r,c]=size(points);
for i=1:c
distance(i)=sqrt((points(1,i)-CPoint(1,1,1)).^2+(points(2,i)-CPoint(1,2,1)).^2);
end
[value,indis]=min(distance)
hold on
scatter(points(1,indis),points(2,indis),'blue','LineWidth',2,'Marker','*');

답변 (2개)

Walter Roberson
Walter Roberson 2013년 2월 17일
Do not loop. Use
distance = sqrt( (points(1,:) - CPoint(1,1,1)).^2 + (points(2,:) - CPoint(1,2,1)).^2 );
  댓글 수: 1
Volkan Kandemir
Volkan Kandemir 2013년 2월 17일
ok this might be helpfull but still is there a event data that gives me object that mause click instead of mause coordinate.

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


bym
bym 2013년 2월 17일
You can try this:
clc;clear;close all
x = rand(100,2);
plot(x(:,1),x(:,2), 'b.')
xlim([-.25,1.25])
ylim([-.25,1.25])
hold on
[u,v] = ginput(1);
[trash,idx] = min(hypot(x(:,1)-u,x(:,2)-v));
plot(x(idx,1),x(idx,2),'ro')

카테고리

Help CenterFile Exchange에서 Map Display에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by