Interpolate

6 views (last 30 days)
Mariska Kret
Mariska Kret on 30 Apr 2011
Hi,
I could use some help with a script that I am writing. I have one excel file with 4100 rows and 225 columns consisting of numbers. In case of one or multiple zeros, I want to interpolate these values with the neighbouring numbers. What I did:
function data = interp_datatest(data, method)
orig_valid = find(data(:,:)); %to interpolate all columns and rows
% to test not-0 values
valid_test = orig_valid(1 : end-1); %I cannot use the first and the last datapoint because there are no neighbouring numbers
% to interpolate indices
calc = valid_test(data (valid_test,:)== 0);
% which values are valid?
% first I make a logical array with only zeros
valid = zeros(length(data),1);
% make the not 0-values 1
valid(orig_valid) = 1; % everything with the number '1' is valid and not a zero.
valid(calc) = 0;
valid = logical(valid);
calc = ~valid; % invert valid values for the to be interpolated %values
% interpolate with methode 'spline'
data(calc,:) = interp1(data(valid,1), data(valid,:), data(calc,1), method);
%Loop for all the columns
[rows,columns] = size(data);
for idx = 1:columns
end
There are mistakes in the script. Please help me find them. Another thing I would like to add but I don't know how is that if there are more than 5 consecutive zeros, I do not want to interpolate and leave the zeros.
Thanks a lot for your comments! M

Answers (2)

Teja Muppirala
Teja Muppirala on 30 Apr 2011
Here's one possible way to do it. I've handled the 5 consecutive zeros thing by using IMCLOSE, a function from the Image Processing Toolbox if you have it.
% This is used to find regions with >= 5 consective zeros
mask = imclose(data,[1;1;1;1;1]);
for col = 1:size(data,2);
%Find the locations and values of nonzero data
[valid,~,values] = find(data(:,col));
%Do not use zeros at the end or the beginning
valid_zeros = intersect(valid(1):valid(end),find(~data(:,col)));
%Call interp1
data(valid_zeros,col) = interp1(valid,values,valid_zeros,method);
end;
data = data.*mask;

Mariska Kret
Mariska Kret on 30 Apr 2011
Thnx for the reply. When I run your script, I get the following errror:
??? Error: File: interpolate_excelfile.m Line: 6 Column: 13 Expression or statement is incorrect--possibly unbalanced (, {, or [.
M
  3 Comments
Walter Roberson
Walter Roberson on 30 Apr 2011
Mariska, try replacing the ~ on line 6 with an otherwise unused variable name, e.g.,
[valid,unused,values] = find(data(:,col))
Teja Muppirala
Teja Muppirala on 30 Apr 2011
Ah. Good call! Sometimes you take these things for granted...

Sign in to comment.

Categories

Find more on Interpolation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!