How do you draw a line between points in matlab?

63,241

Solution 1

If you can organize the x and y coordinates of your line segments into 2-by-N arrays, you can use the function PLOT to plot each column of the matrices as a line. Here's a simple example to draw the four lines of a unit square:

x = [0 1 1 0; ...
     1 1 0 0];
y = [0 0 1 1; ...
     0 1 1 0];
plot(x,y);

This will plot each line in a different color. To plot all of the lines as black, do this:

plot(x,y,'k');

Solution 2

Use plot. Suppose your two points are a = [x1 y1] and b = [x2 y2], then:

plot([x1 x2],[y1 y2]);

Solution 3

use this function:

function [] = drawline(p1, p2 ,color)
%enter code here
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 , color)

call it like:

drawline([fx(i) fy(i)] ,[y(i,1) y(i,2)],'red')

Credit: http://www.mathworks.com/matlabcentral/answers/108652-draw-lines-between-points#answer_139175

Solution 4

If you meant by I'm looking to create a "web" between a set of points where the data tells whether there is a link between any two points actually some kind of graph represented by its adjacency matrix (opposite to other answers simple means to connect points), then:

this gplot function may indeed be the proper tool for you. It's the basic visualization tool to plot nodes and links of a graph represented as a adjacency matrix.

Solution 5

Lets say you want a line with coordinates (x1,y1) and (x2,y2). Then you make a vector with the x and y coordinates: x = [x1 x2] and y=[y1 y2]. Matlab has a function called 'Line', this is used in this way: line(x,y)

Share:
63,241
Sind
Author by

Sind

Updated on May 20, 2020

Comments

  • Sind
    Sind almost 4 years

    I'm looking to create a "web" between a set of points where the data tells whether there is a link between any two points.

    The way I thought of would be by plotting every couple points, and overlaying each couple on top of eachother.

    However, if there is a way to just simple draw a line between two points that would be much easier.

    Any help would be appreciated!