MATLAB: How to remove NaN elements from a matrix and use the matrix in a loop to interpolate

for loopinterpolationnan

Hi,
I have 5 sets of data for 5 curves. I wanna use "interp1" function to interpolate them but my data have NaNs elements at the end of each column and they are not symmetric, for example:
0.7933 0.7654 0.6721 0.5540 0.5174
0.8000 0.8000 0.6901 0.5667 0.5341
NaN NaN 0.7101 0.5901 0.5435
NaN NaN 0.7321 0.5987 0.5622
NaN NaN 0.7388 0.6154 0.5808
NaN NaN 0.7608 0.6234 0.5989
NaN NaN 0.7875 0.6321 0.6423
NaN NaN 0.8000 0.6408 0.6790
NaN NaN NaN 0.6601 0.7177
NaN NaN NaN 0.6721 0.7504
NaN NaN NaN 0.6855 0.7938
NaN NaN NaN 0.7068 0.800
NaN NaN NaN 0.7209 NaN
All columns for xdata start with a value equals to 0.02 and end in 0.8. But here I provided an example to occupy less space.
I wanna get rid of them to reduce the number of command lines for the interpolation part. I have tried to remove them 1 by 1 but it is not efficient. I tried to make a loop but those NaNs values made a big trouble because the size of each column changes in each iteration then I can't have a final matrix at the end to store my data into it (ypp).
%set od data
datasetA=xlsread('pointA');
x=datasetA(:,[1,4,7,10,13]);
y=datasetA(:,[2,5,8,11,14]);
x1= x(:,1);
x2= x(:,2);
x3= x(:,3);
x4= x(:,4);
x5= x(:,5);
x1(any(isnan(x1),2),:) = [];
x2(any(isnan(x2),2),:) = [];
x3(any(isnan(x3),2),:) = [];
x4(any(isnan(x4),2),:) = [];
x5(any(isnan(x5),2),:) = [];
y1= y(:,1);
y2= y(:,2);
y3= y(:,3);
y4= y(:,4);
y5= y(:,5);
y1(any(isnan(y1),2),:) = [];
y2(any(isnan(y2),2),:) = [];
y3(any(isnan(y3),2),:) = [];
y4(any(isnan(y4),2),:) = [];
y5(any(isnan(y5),2),:) = [];
%spline interpolation
xp=linspace(0.02,0.8,100);
yp1= interp1(x1,y1, xp,'spline');
yp2= interp1(x2,y2, xp,'spline');
yp3= interp1(x3,y3, xp,'spline');
yp4= interp1(x4,y4, xp,'spline');
yp5= interp1(x5,y5, xp,'spline');
figure;
plot(x,y,'o',xp, yp1,':.');
hold on
plot(xp, yp2,':.');
plot(xp, yp3, ':.');
plot(xp, yp4,':.');
plot(xp, yp5,':.');
hold off
The result:
untitled.jpg
My loop:
xp=linspace(0.02,0.8,100);
ypp=zeros;
for i=1:5
xx= x(:,i);
yy= y(:,i);
x_nan= xx(~isnan(xx)); % removing NaNs from xdata & ydata
y_nan= yy(~isnan(yy));
yp= interp1(x_nan,y_nan, xp,'spline');
ypp=yp;
end
plot(...);
Many thanks in advance.

Best Answer

OK I found my answer.
Thank you All.
%set od data
datasetA=xlsread('pointA');
%matrix with NaNs values for x and y
x=datasetA(:,[1,4,7,10,13]);
y=datasetA(:,[2,5,8,11,14]);
xp=linspace(0.02,0.8,100);
for i=1:5
xx= x(:,i);
yy= y(:,i);
x_nan= xx(~isnan(xx)); % removing NaN values and keeping the rest
y_nan= yy(~isnan(yy));
yp= interp1(x_nan,y_nan, xp,'spline');
ypp{i}=yp; %storing the result for Interpolated Y
clear x_nam y_nam yp
end
figure;
hold on
plot(x,y,'o');
for n=1:5
plot(xp,ypp{n});
end
hold off