Filling incomplete data with cftool

8 ビュー (過去 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 Center および File ExchangeInterpolation についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by