How to keep NaNs in interp?

53 views (last 30 days)
Sancheet Hoque
Sancheet Hoque on 26 Jan 2017
Edited: Matt J on 26 Jan 2017
I have an array that contains both NaNs and values. I'm plotting this values to compare to another dataset and if there is a long string of NaNs, interp simply ignores them and interpolates between the point before the first NaN and the point after the last NaN. This kind of throws off my comparison and I would rather have more NaNs so it's blank in the plot to show that no data was not found. Below is the code I used to interpolate array u (array u is the picture shown below). After I interpolated it the warning sign below showed. In the plot I attached the red line is the plot after it has been interpolated. I do not want a constant line like that, but rather an empty space wherever a NaN is, but the NaNs are removed during the interpolation. Is there a way to just interpolate the values and just increase the number of NaNs in the new interpolation? The picture with the datenums is the tarray. tnew is a little large, but I have inserted the code that creates it.
N = (numel(tarray)-1)*359 + numel(tarray);
t_new = linspace(tarray(1),tarray(end),N).';
u_newp = interp1(tarray,u,t_new,'pchip');
plot(t_new,u_new,'-b')
hold on
plot(t_new,u_newp,'-r')
title(sprintf('Path Comparasion of %s', baseFileName))
xlabel('Time')
ylabel('East Velocity m/s')
hold off
datetick('x','HH:MM:SS','keepticks')
legend('HF Data','Hycom Prediction')
Warning: Columns of data containing NaN values have been ignored during interpolation.
> In interp1>Interp1DStripNaN (line 241)
In interp1 (line 171)
In PathComp (line 119)
  4 Comments
Steven Lord
Steven Lord on 26 Jan 2017
So what you've shown is u? Can you also show tarray and t_new?
Sancheet Hoque
Sancheet Hoque on 26 Jan 2017
yea the array with the NaNs is u, while the datenum one I just inserted is the tarray. tnew is 5671x1 double array

Sign in to comment.

Accepted Answer

Matt J
Matt J on 26 Jan 2017
Edited: Matt J on 26 Jan 2017
Couldn't you just put the NaNs back in u_newp?
u_newp(idx)=nan;
  2 Comments
Matt J
Matt J on 26 Jan 2017
Edited: Matt J on 26 Jan 2017
To obtain the locations 'idx', I would interpolate without 'pchip'. This is NaN preserving,
u_newp = interp1(tarray,u,t_new,'pchip');
idx = isnan(interp1(tarray,u,t_new));
u_newp(idx)=nan;
Sancheet Hoque
Sancheet Hoque on 26 Jan 2017
Thank you! It worked perfectly.

Sign in to comment.

More Answers (0)

Categories

Find more on Interpolation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!