Draw lines between points
조회 수: 218 (최근 30일)
이전 댓글 표시
I have a text file ("coordinates.txt") that contains x and y coordinates of points.
File looks like this:
11 44 2 9
11 44 5 8
2 1 6 11
2 1 10 3
I need to draw lines between (11, 44) to (2, 9), (11, 44) to (5, 8), (2, 1) to (6, 11), and (2, 1) to (10, 3).
I can do this by manually like:
x = [11 11 2 1; 2, 5, 6, 10];
y = [44 44 1 1; 9 8 11 3];
plot (x, y).
But the actual file is quite long and I need to "automate" this process.
I tried:
load coordinates.txt;
edit coordinates.txt;
x1= [coordinates(:, 1); coordinates(:, 3)];
y1 = [coordinates(:, 2); coordinates(:, 4)];
plot (x1, y1).
It gives me lines drawn from (11, 44) to (2, 9), (2, 9) to (5, 8), (5, 8) to (6, 11), (6, 11) to (10, 3).
Could anyone help?
댓글 수: 0
채택된 답변
dpb
2013년 12월 4일
Simply read in the array as it is in the file, then if
m =
11 44 2 9
11 44 5 8
2 1 6 11
2 1 10 3
for instance,
>> x=[m(:,1) m(:,3)];
>> y=[m(:,2) m(:,4)];
>> plot(x',y')
Or, you can do w/o the temporaries x, y of course if there's no other need to separate.
NB: I'm presuming the actual example x
x = [11 11 2 1; 2, 5, 6, 10];
contains a typo given the content of the file in that the '1' on the first line should also be a '2'...
추가 답변 (2개)
Kelly Kearney
2013년 12월 4일
Matlab considers each column passed to plot to represent a separate line segment. So just transpose your data:
plot(x1', y1')
댓글 수: 0
Bandar
2014년 6월 2일
편집: Bandar
2014년 6월 2일
In this case you better build your own function to draw a line given two points.
function [] = drawLine(p1, p2)
theta = atan2( p2(2) - p1(2), p2(1) - p1(1));
r = sqrt( (p2(1) - p1(1))^2 + (p2(2) - p1(2))^2);
line = 0:0.01: r;
x = p1(1) + line*cos(theta);
y = p1(2) + line*sin(theta);
plot(x, y)
in the main function do the following
>> drawLine([0 0], [-5 5])
>> hold on
>> drawLine([0 0], [5 5])
For many points just put `drawLine` in a loop. Hope this helps
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Annotations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!