How to show selected points and connect first point to last point?

조회 수: 21 (최근 30일)
Mariam Shahab
Mariam Shahab 2022년 9월 26일
댓글: Mariam Shahab 2022년 10월 1일
Hi all,
I have the following code:
[x,y]=ginput(); % user can chose any number of coordinates by clicking on graph
plot(x,y);
Is there a way that when the user clicks in the graph, the selected points are marked (to show the selected points)?
Lastly, I want the first point to connect to the last point, but I am not sure how to do this.
I will appreaciate if someone can help me out with my two inquiries. Many thanks!
  댓글 수: 2
Geoff Hayes
Geoff Hayes 2022년 9월 26일
@Mariam Shahab - if you want the last point to be connected to the first point, then the easiest way to do this might be to append the first points' x and y coordinates to the x and y arrays respectively. To show which points, have been selected, just change the call to plot so that you incoude a shape at that point. For example
plot([x ; x(1)],[y ; y(1)], 'bo-');
might do what you want.
Mariam Shahab
Mariam Shahab 2022년 9월 26일
Thank you. This was what I was looking for.

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

채택된 답변

Daksh
Daksh 2022년 9월 29일
Hi
It is my understanding that you wish to view marked points in real time as the user selects multiple points in a plot using "ginput" and you want all points connected, all the way from the first to the last in order through a line.
The following code illustatres how to achieve the same; I've assumed you know the number of points to be marked beforehand (I've taken 5 points in this case) and I've also put the number tag string on each marked point to depict the order of point marking:
clear all
close all
%assuming you want to plot 5 points for example
n=5;
a=zeros(n,1);
b=zeros(n,1);
%running a loop over all selected points
for i=1:5
%allowing user to click a point to choose, x,y store the coordinates
[x,y] = ginput(1);
%text depiction for each point, with "i" as the point marker
h1 = text(x,y,int2str(i), ...
'HorizontalAlignment','center', ...
'Color', [1 0 0], ...
'FontSize',8);
a(i)=x;
b(i)=y;
end
hold on
%plotting a line between all points
plot([a ; a(1)],[b ; b(1)], 'b-');
hold off
Hope it helps

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by