Connecting points on a line

17 views (last 30 days)
dhhtr
dhhtr on 8 May 2015
Commented: dhhtr on 8 May 2015
Hello Community,
I have plotted some data points in MATLAB however, I would like some help as to how I may go about in connecting the points. Since I know the exact location of each point, would I have to use the plot function to draw a line from point A to B (and if so, how?), or is there a more efficient method I can about out to achieve the same results?
Ultimately, I would like to connect this to represent a tree topology configuration. I have looked into the built in treeplot function. Although this helps plot my data in a general tree network topology, it does not factor into the distance from each point and does not help me address the problem.
Many thanks, MATLAB Rookie

Accepted Answer

Walter Roberson
Walter Roberson on 8 May 2015
plot() is often the easiest way. But you could also create line() objects directly.
Sometimes for efficiency it is worth using the fact that if you include NaN in a list of y values, the line that is being drawn between the points will break, and will pick up at the next non-NaN value. For example
plot([x1 x2], [y1 y2]);
hold on
plot([x3 x4], [y3 y4]);
could be used to create line segments connecting y1 to y2, and y3 to y4, but nothing from y2 to y3. Completely independent line() objects would be created, each of which could be controlled for callbacks and colors and the like. But sometimes you just need a visual gap and do not mind that everything will work together. In such a case you can use
plot([x1 x2 nan x3 x4], [y1 y2 nan y3 y4]);
the NaN tells the plotting routine to not visually connect the part before with the part after.
  1 Comment
dhhtr
dhhtr on 8 May 2015
Thank you kindly, Walter! That was very helpful.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!