If I have a plotted curve with a small gap in the midlle, how can I fill the gap with a straight line?

1 visualización (últimos 30 días)
If I have a plotted curve with a small gap in the midlle, how can I fill the gap with a straight line?

Respuesta aceptada

Star Strider
Star Strider el 29 de Dic. de 2018
I have no idea what your curve is or the reason the gap exists, so this is a guess. The approach to both is to find the ends of the first part of the curve, and connect them to the beginning of the second part.
If you know where the gap is, try this:
x1 = linspace(0, 1);
y1 = sin(x1*3*pi);
x2 = x1 + 2;
y2 = cos(x2*4*pi);
figure
subplot(2,1,1)
plot(x1, y1, 'g')
hold on
plot(x2, y2, 'g')
hold off
subplot(2,1,2)
plot(x1, y1, 'g')
hold on
plot(x2, y2, 'g')
plot([x1(end) x2(1)], [y1(end) y2(1)], '-r') % Connect Ends With Red Line
hold off
If your data have a region of one or more consecutive NaN values, try this:
x3 = linspace(0, 5);
x3(40:60) = NaN;
y3 = exp(-(x3-2.5).^2);
nanv = find(isnan(y3)); % Find Region With ‘NaN’ Values
figure
plot(x3, y3, 'g')
hold on
plot(x3([nanv(1)-1, nanv(end)+1]), y3([nanv(1)-1, nanv(end)+1]), 'r') % Connect Ends With Red Line
hold off

Más respuestas (0)

Categorías

Más información sobre Interpolation en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by