Here's how:
Copy% Your two points
P1 = [0,0,0];
P2 = [13,-11,19];
% Their vertial concatenation is what you want
pts = [P1; P2];
% Because that's what line() wants to see
line(pts(:,1), pts(:,2), pts(:,3))
% Alternatively, you could use plot3:
plot3(pts(:,1), pts(:,2), pts(:,3))
Admittedly, this might seem a bit counter-intuitive at first, but in the long run it'll make sense.
If you read doc plot or doc line, you'll see that each expects sets of x, y and z data, respectively. That is, using
Copyplot3(X,Y,Z)
with X, Y and Z some matrices, plot3 will draw a line from the first triplet (X(1) Y(1) Z(1)) to the second triplet (X(2) Y(2) Z(2)) and so on -- same for line.
MathWorks
mathworks.com › matlab › graphics › 2-d and 3-d plots › line plots
plot3 - 3-D line plot - MATLAB
Create plots by passing a table to the plot3 function followed by the variables you want to plot. When you specify your data as a table, the axis labels and the legend (if present) are automatically labeled using the table variable names. axis | scatter3 | bar3 | tiledlayout | nexttile | grid ...
MathWorks
mathworks.com › matlabcentral › answers › 862525-how-can-i-draw-a-line-on-a-3d-plot
How can I Draw a Line on a 3D plot? - MATLAB Answers - MATLAB Central
June 22, 2021 - but the xline command will not work for the 3D plot, as it does not follow the Z data and draws a line straight through the dipole I am modelling. "value" in the above code is whatever line is selected by the app user. ... Does anyone know how to plot a red line like this that will contour to my Z data? Here is the code for my 3D plot below: ... Any help is appreciated, thank you. ... https://www.mathworks.com/matlabcentral/answers/862525-how-can-i-draw-a-line-on-a-3d-plot#comment_1597970
Drawing line segments in a 3d plot
Drawing line segments in a 3d plot. Learn more about plot3 MATLAB More on mathworks.com
How to draw 3d Line plot
How to draw 3d Line plot . Learn more about 3dplot More on mathworks.com
How to draw a horizontal line in a diagram (3D plot)?
Hello, I want to draw a horizontal line in my diagram! But it does not work. (Its a 3d plot with the view from above) Thank you for your support. *Coordinates: x1=-350, y1=60, z1=10^6; x2=-10... More on mathworks.com
Plot a line in 3D
Define a vector T: T =-100:.1:100 Apply T in your coordinates, u will get a vector for each one. plot3 your coordinates vector Outline a limit for 3 axis And u r done. More on reddit.com
Videos
MathWorks
mathworks.com › matlabcentral › answers › 439275-how-to-draw-3d-line-plot
How to draw 3d Line plot - MATLAB Answers - MATLAB Central
January 11, 2019 - https://www.mathworks.com/matlabcentral/answers/439275-how-to-draw-3d-line-plot · Cancel Copy to Clipboard · Answered: madhan ravi on 11 Jan 2019 · Accepted Answer: madhan ravi · I want to draw this kind of figure. So I made some code like below. time1 and ak1 is '2500x1' vector. and 7 is y axix that i want to put. But it doesn't work well. What kind of plot should I use in Matlab?
MathWorks
mathworks.com › matlabcentral › answers › 308422-how-to-draw-a-horizontal-line-in-a-diagram-3d-plot
How to draw a horizontal line in a diagram (3D plot)? - MATLAB Answers - MATLAB Central
October 21, 2016 - I want to draw a horizontal line in my diagram! But it does not work. (Its a 3d plot with the view from above) Thank you for your support. Coordinates: x1=-350, y1=60, z1=10^6; x2=-100, y2=60, z2=10^6 ... https://www.mathworks.com/matlabcentral/answers/308422-how-to-draw-a-horizontal-line-in-a-diagram-3d-plot#comment_400441
Reddit
reddit.com › r/matlab › plot a line in 3d
r/matlab on Reddit: Plot a line in 3D
November 6, 2022 -
I have the parametric form of a line in 3d-space. I want to plot it in 3D but it appears short. I am doing something wrong ?
code:
xt = @(t) t+5;
yt = @(t) 4*t+1;
zt = @(t) -2*t+3;
ezplot3(xt,yt,zt,[-1,1],1000)
Top answer 1 of 3
3
Define a vector T: T =-100:.1:100 Apply T in your coordinates, u will get a vector for each one. plot3 your coordinates vector Outline a limit for 3 axis And u r done.
2 of 3
2
Also, your line is "too short" in that code, cuz u r defining a limit of [-1,1] and your graph has a bigger limit in the axis. Either u enlarge your [-1,1] limit, or define proper limits for the axis (e.g. all of them between -1.1 and 1.1).
MathWorks
mathworks.com › matlabcentral › answers › 531288-plotting-a-3d-line-on-a-surface-plot
Plotting a 3D line on a surface plot - MATLAB Answers - MATLAB Central
May 23, 2020 - I want to plot a 3D line y = A.*((1+(9/4).*x).^(3/2)-1); z = x.^(3/2); on the surface z = x.^(3/2); so basically I want two plots one surface and other a line. Here's the code that I wrote...
MathWorks
mathworks.com › matlab › graphics › labels and styling › labels and annotations
line - Create primitive line - MATLAB
Unlike the plot function, the line function does not call newplot before plotting and does not respect the value of the NextPlot property for the figure or axes. It simply adds the line to the current axes without deleting other graphics objects or resetting axes properties. However, some axes properties, such as the axis limits, can update to accommodate the line. ... This function accepts GPU arrays, but does not run on a GPU. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
MathWorks
mathworks.com › matlabcentral › answers › 82310-add-some-data-line-to-3d-plot
Add some data line to 3D plot - MATLAB Answers - MATLAB Central
July 17, 2013 - Add some data line to 3D plot. Learn more about plot3, data line, construction line
MathWorks
mathworks.com › matlabcentral › answers › 19734-3d-line-plot
3D line plot - MATLAB Answers - MATLAB Central
October 28, 2011 - Sign in to answer this question. ... If you want to plot 3D lines, you need to have 3D data, whether the data is the typical x,y,z coordinates or one of the dimension is time.
MathWorks
mathworks.com › matlabcentral › answers › 378548-3d-line-plot-between-coordinates
3D line plot between coordinates - MATLAB Answers - MATLAB Central
January 23, 2018 - However, it adds one lines from (0,-6,24) to (18,-6,0). Anyone have an idea of how to make the function only draw the number of lines specified from the MAT file? ... Sign in to comment. Sign in to answer this question. ... https://www.mathworks.com/matlabcentral/answers/378548-3d-line-plot-between-coordinates#answer_301318
MathWorks
mathworks.com › matlab mobile › matlab mobile fundamentals
Creating 3-D Plots - MATLAB & Simulink
This example shows how to create 3-D line plots in MATLAB using the plot3 function.
MathWorks
mathworks.com › matlabcentral › answers › 455331-add-lines-through-origin-in-3d-scatter-plot
add lines through origin in 3D scatter plot - MATLAB Answers - MATLAB Central
April 9, 2019 - There is no zline() funciton built into matlab (2019a). Note that xline and yline became available in 2018b. xline and yline plot the reference lines in the middle of the z axis. To plot the origin lines (or any other 3D reference lines),
MathWorks
mathworks.com › matlabcentral › answers › 370535-plotting-a-straight-line-in-3d
Plotting a straight line in 3D - MATLAB Answers - MATLAB Central
December 2, 2017 - Plotting a straight line in 3D. Learn more about image processing, plotting straight line