If I have a plotted curve with a small gap in the midlle, how can I fill the gap with a straight line?
1 view (last 30 days)
Show older comments
Valentina Malconi
on 29 Dec 2018
Answered: Star Strider
on 29 Dec 2018
If I have a plotted curve with a small gap in the midlle, how can I fill the gap with a straight line?
0 Comments
Accepted Answer
Star Strider
on 29 Dec 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
0 Comments
More Answers (0)
See Also
Categories
Find more on Interpolation in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!