How to prevent the connecting of dots between two data points when the middle points are missing in plot

43 views (last 30 days)
Hello Everyone.
I have a question regarding using plot in Matlab. Consider that I have the following arrays and their corresponding plots:
x1=1:10;
y1=sin(x1*2*pi/18);
x2=x1;
x2(5:6)=[];
y2=sin(x2*1.8*pi/18);
figure(1)
plot(x1,y1,'-bx',x2,y2,'-ro')
As you can see, in the red plot, the data for x=5 and x=6 are missing. What I need, is to plot these two curves in a way, that if we do not have data in a point, the line becomes separated, i.e. I do not want the Matlab to connect the data points between x=4 and x=7 in the above exmple. I have data with different density in various points that are required to be plotted at a same figure.
Now I know that there is a scatter plot, but I need to you plot command. Also, my real data is much more complicated and I cannot separate each curve to separate curves and plot them, as I need them to be one curve when I select them after plotting. In other words it is not possible for me to do the following:
figure(2)
plot(x1,y1,'-bx',x2(1:4),y2(1:4),'-ro',x2(5:end),y2(5:end),'-ro')
The figure(2) shows what I want visually. The problem is that when you select the curves in figure(2), there are three curves and, the curve for x2(1:4) is different from the curve for x2(5:end). I need them to remain as one curve when selected.
Do you know a way to solve this problem?

Accepted Answer

Kevin Holly
Kevin Holly on 6 May 2024
x1=1:10;
y1=sin(x1*2*pi/18);
x2=x1;
x2(5:6)=NaN;%Change this to NaN
y2=sin(x2*1.8*pi/18);
figure(1)
plot(x1,y1,'-bx',x2,y2,'-ro')

More Answers (0)

Community Treasure Hunt

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

Start Hunting!