Connecting dots with straight line in a loop.

조회 수: 10 (최근 30일)
Tatte Berklee
Tatte Berklee 2019년 10월 20일
답변: Image Analyst 2019년 10월 20일
Hi all,
I have a question regarding connecting the dots.
Suppose I have the following sample data points:
example.png
I have a while-loop that identifies the first, second, and fourth points in the figure.
I know their index numbers and their (x,y) values.
Pictorially, I would like to connect with a straight line, the aforementioned points.
What is the best way of doing this?
My gut feeling is to input the "draw" code within the while-loop I have so each time I obtain the index of the next point I want to connect, I simply connect to that pictorially. But how to go about this?
Thanks in advance!

채택된 답변

Image Analyst
Image Analyst 2019년 10월 20일
It seems that no one is exactly sure what you want to do, since the super obvious plot(x,y,'b-') doesn't seem to be it.
So I'll throw out a couple of guesses.
To close the figure (if that's what you want) you can tack the first point onto the array:
xp = [x, x(1)];
yp = [y, y(1)];
plot(xp, yp, 'b.-', 'LineWidth', 2, 'MarkerSize', 18);
grid on;
To make a bunch of points in between, you'd have to use polyfit and polyval() for each segment. Here it is for one segment with endpoints (x1, y1) and (x2, y2):
numNewPoints = 100; % Whatever you want.
newx = linspace(x1, x2, numNewPoints);
y = linspace(y1, y2, numNewPoints);
coefficients = polyfit([x1,x2], [y1, y2], 1);
newY = polyval(coefficients, newx);

추가 답변 (2개)

darova
darova 2019년 10월 20일
Just use plot function
plot(x(index),y(index))
  댓글 수: 2
Tatte Berklee
Tatte Berklee 2019년 10월 20일
Hi! But how does the plot make the dots connect? Not only do I want the dots appear on the plot but also connect to the subsequent dot I obtain.
darova
darova 2019년 10월 20일
If you have data (x,y) and it's order just use plot
x = rand(1,4);
y = rand(1,4);
index = [1 4 2 3];
plot(x(index),y(index))

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


ABHILASH SINGH
ABHILASH SINGH 2019년 10월 20일

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by