Draw lines between points
124 次查看(过去 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!