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!