40 views (last 30 days)

Hello,

I would like to interpolate the missing data on the graph marked by NaN.

when I try it it does not work.

the code is:

filteredData = data(:,2);

vector = [];

for index = 1:length(filteredData)

if filteredData(index) >= 2048

filteredData(index) = NaN;

vector = [vector index];

end

end

index2 = 2;

while index2 < length(filteredData)

if filteredData(index2) < filteredData(index2-1) - 750 && filteredData(index2) < filteredData(index2+1) - 750

vector = [vector index2];

filteredData(index2) = NaN;

index2 = index2 + 1;

end

if filteredData(index2) > filteredData(index2 - 1) + 750 && filteredData(index2) > filteredData(index2+1) + 750

vector = [vector index2];

filteredData(index2) = NaN;

index2 = index2 + 1;

end

index2 = index2 + 1;

end

vector = sort(vector);

y = interp1(1:length(filteredData),filteredData,vector, "linear");

plot(vector, y, '^r');

format long g;

Star Strider
on 27 Oct 2020 at 17:37

Star Strider
on 28 Oct 2020 at 1:11

‘how do I delete NaN?’

The NaN values are deleted in this assignment:

Dataq = Data(~nanrows,:);

The ‘nanrows’ variable is a logical vector that is true (or 1) where the row has a NaN value and is 0 otherwise. The tilde operator (~) negates that, so this assigns to ‘Dataq’ all the rows that do not have NaN values. See the documentation section on Matrix Indexing for a full explanation.

‘by the way filteredData is only one column of data as only the second column was needed and not all 4.’

If you want to interpolate only the second column, just choose it and the first column (the independent variable) as the ‘Data’ matrix, and use the rest of my code as provided.

KSSV
on 27 Oct 2020 at 17:24

Edited: KSSV
on 27 Oct 2020 at 17:24

Read about fillmissing.

KSSV
on 27 Oct 2020 at 17:39

Then you have to use like below:

n = 100 ;

x = 1:n ;

t = rand(size(x)) ;

% make some values NaN to fill

y = t ;

idx = sort(randperm(n,20)) ;

y(idx) = NaN ;

% fill nan using interp1

xi = setdiff(x,idx) ;

yi = y(~isnan(y)) ;

y(idx) = interp1(xi,yi,idx) ;

plot(x,t,'r',x,y,'b')

Actually using random data for demo is not good. But you can follow the procedure.

Opportunities for recent engineering grads.

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

Start Hunting!
## 0 Comments

Sign in to comment.