If I have a plotted curve with a small gap in the midlle, how can I fill the gap with a straight line?
1 次查看(过去 30 天)
显示 更早的评论
If I have a plotted curve with a small gap in the midlle, how can I fill the gap with a straight line?
0 个评论
采纳的回答
Star Strider
2018-12-29
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interpolation 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!