MATLAB: 1d interpolation from multiple text files

bilinear interpolationinterpolationMATLABMATLAB C/C++ Math Librarynearest neighbor techniquetext file

I want to apply these two interpolations for missing temperature data in text files
  1. nearest neighbor interpolation
  2. bilinear interpolation
My text files are named with Output_00 to Output_23 in a folder. Each text file contains three columns
  • First column is latitude
  • Second column is longitude
  • Third column consist on air temperature corresponding to that latitude and longitude.
In temperature column there are -9999.000 values which representing missing or NaN values.
I want to interpolate these values with above mentioned techniques in such a way that
  • Each interpolation technique interpolate temperature(3rd column) in text file and save this interpolated text file with inter_NNH(means this text file has been interpolated with nearest neighbor) and inter_BILIN(means this text file has been interpolated with bi-linear interpolations).
  • After that, there will be 72 text files remains in my folder. (24 for uninterpreted and 48 of interpolated.)
I able to attached ten text files(due to size limit) with this question. If any more requirement need tell me first.
Thanks in advance for this kind effort.

Best Answer

Although beginners love using numbered variables and think that using numbered variables will solve all of their problems they actually just make writing code more difficult. To process these you will need to either write lots of similar lines by hand, or use dynamic variable names. These are both BAD ways to program! No matter how obvious it seems to beginners, they need to learn to avoid using numbered variables. Here is why:
Beginners need to learn that if they are numbering their variables then this is a de facto index, and that they should actually use a real index into just one variable. Then your task becomes trivial to achieve. The reason that you are stuck now is because you have chosen the slow and amateur way of coding, by using numbered variables, which makes the task very difficult.
Here is some simple code which reads the files into one variable, and then rearranges it for you to run your interpolation on:
% Read the file data:
S = dir('*Output_*.txt');
N = sort({S.name});
P = numel(N);
R = size(load(N{1},'-ascii'),1);
M = zeros(R,3,P);
for k = 1:P
M(:,:,k) = load(N{k},'-ascii');
end
tmp = 0==diff(M(:,1:2,:),1,3);
assert(all(tmp(:)),'First columns do not match')
% Rearrange:
[~,~,X] = unique(M(:,1,1));
[~,~,Y] = unique(M(:,2,1));
Z = reshape(M(:,3,:),max(Y),max(X),P);
% Detect -9999
idx = Z<-9998;
% Try your interpolation here!