Filling incomplete data with cftool

1 vue (au cours des 30 derniers jours)
Nicolas Silva Schmalbach
Nicolas Silva Schmalbach le 27 Sep 2021
Modifié(e) : William Rose le 28 Sep 2021
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.

Réponse acceptée

William Rose
William Rose le 28 Sep 2021
Modifié(e) : William Rose le 28 Sep 2021
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')

Plus de réponses (1)

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

Catégories

En savoir plus sur Interpolation dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by