Plot a graph that interpolates between NaNs?

조회 수: 15 (최근 30일)
Grant
Grant 2017년 4월 11일
댓글: Grant 2017년 4월 11일
I'm plotting a line graph with two y vectors against an x vector (using plotyy). Both of the y vectors have data gaps as NaNs. When I plot I do not want there to be gaps on the graphs where there are NaNs, I want it to connect all the 'real values'.
A solution offered for others is to interpolate the values before plotting. This DOES NOT work for me because I want to have a marker point at each of the real values. I attach a picture of what I want created in Excel.
------ For simplicity I've described the problem as above and not provided code. In fact I'm plotting against 3 y-axis but the 3rd y-vector does not have NaNs. I'm using the user-created function plotyyy.m which itself calls MATLAB's function plotyy.
Thank you
  댓글 수: 2
KSSV
KSSV 2017년 4월 11일
Using interp1 you can fill your gaps/ NaN's.
Grant
Grant 2017년 4월 11일
This doesn't work
See above: "A solution offered for others is to interpolate the values before plotting. This DOES NOT work for me because I want to have a marker point at each of the real values. "

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2017년 4월 11일
편집: Stephen23 2017년 4월 11일
So remove the NaNs:
id1 = ~isnan(y1);
id2 = ~isnan(y2);
plotyy(x1(id1),y1(id1),x2(id2),y2(id2))
  댓글 수: 4
Stephen23
Stephen23 2017년 4월 11일
편집: Stephen23 2017년 4월 11일
@Grant: My answer does not change your data: the x values are the same, and the corresponding y values are the same. All my answer does is remove the NaNs. The plotted position of any (x,y) pair will not change. Try it.
Grant
Grant 2017년 4월 11일
Thank you very much.
(Apologies, I misunderstood and MATLAB was not available to me at the time)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
Steven Lord 2017년 4월 11일
If you're using release R2016b or later, use the fillmissing function to fill the NaNs (which represent missing data for double and single arrays) using a method of your choice. Call fillmissing with two outputs to both identify the locations of the NaNs and fill them, then use the second output from that call to generate a vector to use for the markerIndices property of your plot. This will allow you to plot markers only for the points that were in your original data set, not the points that were filled.

카테고리

Help CenterFile Exchange에서 Two y-axis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by