Filling incomplete data with cftool

조회 수: 7 (최근 30일)
Nicolas Silva Schmalbach
Nicolas Silva Schmalbach 2021년 9월 27일
편집: William Rose 2021년 9월 28일
Hello, I've been working with data obtained from body signals, but some data values are NaN so the curve has voids in it, I tried to aproximate the curve with cftool, but I don't know how to fix the data matrix and replace the NaN values. I need to keep the same number of data in the matrix.

채택된 답변

William Rose
William Rose 2021년 9월 28일
편집: William Rose 2021년 9월 28일
See attached.
%replaceNaNs.m WCRose 20210928
%Replace NaN values in array with interpolated values.
clear;
%Create simulated data
t=0:.001:1;
x=cos(10*pi*t);
%Add NaNs to the data
for i=1:length(x)
if rand<.3, x(i)=NaN; end
end
fprintf('NaNs in x(): %d out of %d\n',sum(isnan(x)),length(x));
NaNs in x(): 294 out of 1001
%Fix it by interpolation
%1. Determine how many consecutive NaNs at start of array
k1=0;
while isnan(x(k1+1)),k1=k1+1; end
%2. Determine how many consecutive NaNs at end of array
k2=0;
while isnan(x(end-k2)), k2=k2+1; end
%3. Remove beginning and/or end, if they are NaNs.
y=x(1+k1:end-k2);
%4. Replace remaining NaNs by linear interpolation
i=1;
while i<length(y)
if isnan(y(i))
j=1;
while isnan(y(i+j)), j=j+1; end
%Now we know we have j consecutive NaNs, starting at y(i)
%Replace the NaNs by linear interpolation
y(i:i+j-1)=y(i-1)+(1:j)*(y(i+j)-y(i-1))/(j+1);
i=i+j+1;
else
i=i+1;
end
end
fprintf('NaNs in y(): %d out of %d\n',sum(isnan(y)),length(y));
NaNs in y(): 0 out of 997
%5. Plot results
figure;
subplot(2,1,1)
plot(t,x,'.b')
subplot(2,1,2)
plot(t(1+k1:end-k2),y,'.r')

추가 답변 (1개)

Steven Lord
Steven Lord 2021년 9월 27일
Do you want to remove the NaN values with rmmissing, fill them using fillmissing, or do something else with them (and if so what?)

카테고리

Help CenterFile Exchange에서 Interpolation에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by