Is there a way to disconnect lines between data points while utilizing the plot function?

on 22 Oct 2013
Latest activity Commented on by Brad

on 22 Oct 2013
Accepted Answer by sixwwwwww

sixwwwwww (view profile)

Let's say I have the following lines of code;
x = [1;2;3;4;5;6;7;8;6];
y = [10;20;30;40;50;60;70;80;30];
plot(x,y,'-o');
When executed, I get a simple 2D line plot where all 9 of the Y values are plotted and connected by a solid line.
Is there a way to disconnect the line between the 8th and 9th data point while maintaining it for the first 8?

sixwwwwww (view profile)

on 22 Oct 2013
Edited by sixwwwwww

sixwwwwww (view profile)

on 22 Oct 2013

Dear Brad, you can do it the following way:
x = [1;2;3;4;5;6;7;8;9];
y = [10;20;30;40;50;60;70;80;90];
plot(x(1:end - 1),y(1:end - 1),'-o'); hold on
plot(x(end), y(end), 'o')
xlim([x(1) x(end)+x(1)]), ylim([y(1) y(end)+y(1)])
I hope it helps. Good luck!

1 Comment

on 22 Oct 2013
Good call. I forgot all about using hold on!!

Answer by Matt Kindig

Matt Kindig (view profile)

on 22 Oct 2013

Another way is to insert a NaN between the dis-connected points, such as:
x = [1;2;3;4;5;6;7;8;6];
y = [10;20;30;40;50;60;70;80;30];
x = [ x(1:8), NaN, x(9:end)];
y = [ y(1:8), NaN, y(9:end)];
plot(x,y,'-o');
Matlab won't plot a NaN, so the effect is to break up your lines.