Draw Lines between Points in MATLAB

In summary, the conversation is about how to connect points together in MATLAB using the plot() function. The solution is to use plot(V(:,1),V(:,2)) to connect the points in a 2D graph, and plot3() for a 3D graph. In order to only connect adjacent points, the matrix may need to be broken up or a function from the MATLAB File Exchange may need to be used.
  • #1
EngWiPy
1,368
61
Hi,

I have a matrix V, which is a 5-by-2 matrix, where each row represents a point in the xy-plain. How can I connect these points together in MATLAB, please?

Thanks in advance
 
Physics news on Phys.org
  • #2
plot(V(:,1),V(:,2)) should do it, if I remember my matrix slicing.
 
  • #3
jhae2.718 said:
plot(V(:,1),V(:,2)) should do it, if I remember my matrix slicing.

Ya, I tried this, but it does not work. It connects just between three points. I need to connect between all points to form a closed graph, starting from a point and ending at the same point.

Thanks
 
  • #4
It only plots three points? :confused:

I think you should be able to get a connected graph by appending the start point onto the matrix (getting a 6x2) and then using plot.
 
  • #5
jhae2.718 said:
It only plots three points? :confused:

I think you should be able to get a connected graph by appending the start point onto the matrix (getting a 6x2) and then using plot.

Thank you very much. It is now working perfectly.
 
  • #6
Glad to hear it.
 
  • #7
jhae2.718 said:
Glad to hear it.

If I want to extend it to 3D, is it straightforward to do that?
 
  • #8
You should probably be able to do something similar with plot3() for lines in R3.
 
  • #9
jhae2.718 said:
You should probably be able to do something similar with plot3() for lines in R3.

That indeed works, but this connects all the points together. Actually, in 3D I just want to connect the adjacent points only. How can I do that?
 
  • #10
S_David said:
That indeed works, but this connects all the points together. Actually, in 3D I just want to connect the adjacent points only. How can I do that?

I'm not sure what exactly you mean by this...
 
  • #11
jhae2.718 said:
I'm not sure what exactly you mean by this...

Neither do I. But basically, V is a matrix that contains the vertices of a polytope, and I want just to connect the adjacent vertices not all together. I am not sure how to know what are the adjacent vertices from V.

Thanks
 
  • #12
You may have to break the matrix up into matrices such that plot3 will produce the lines connected the way you want.

Another option is to look on the file exchange and see if there's anything you can use there. http://www.mathworks.com/matlabcentral/fileexchange/
 
  • #13
jhae2.718 said:
You may have to break the matrix up into matrices such that plot3 will produce the lines connected the way you want.

Another option is to look on the file exchange and see if there's anything you can use there. http://www.mathworks.com/matlabcentral/fileexchange/

Ok, I will. Thank you.
 
  • #14
I think there may be a file on the exchange that will do what you want, otherwise I'm afraid I don't know of a built in function that will do what you want on only one call.
 

1. How do I draw a straight line between two points in MATLAB?

To draw a straight line between two points in MATLAB, you can use the plot function. First, define the x and y coordinates of the two points in separate arrays. Then, use the plot function with these arrays as inputs, specifying the line style and color if desired. Finally, use the hold on command to ensure the line is drawn on top of any existing plots.

2. Can I draw multiple lines between multiple points in one plot in MATLAB?

Yes, you can use the plot function to draw multiple lines between multiple points in one plot. Simply provide the x and y coordinates of each line in separate arrays, and use the hold on command to ensure all lines are visible on the plot.

3. How can I customize the appearance of the lines in my plot?

You can customize the appearance of the lines in your plot by specifying the line style and color in the plot function. For example, you can use 'r--' to plot a red dashed line, or 'bs-' to plot a blue square-dotted line. You can also add a legend to your plot using the legend function to label each line.

4. Is it possible to draw a curved line between points in MATLAB?

Yes, it is possible to draw a curved line between points in MATLAB using the plot function. Instead of specifying the x and y coordinates of the points, you can use the plot function with a single input argument which is a vector of complex numbers. These complex numbers represent the points on the complex plane, and the plot function will automatically connect them with a curved line.

5. Can I save my plot of lines between points as an image in MATLAB?

Yes, you can save your plot of lines between points as an image in MATLAB by using the saveas function. This function allows you to specify the file name and format of the image (such as png, jpg, or eps). You can also use the print function to save the plot in a specific resolution or size.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
712
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
819
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
969
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
Back
Top