1d interpolation from multiple text files

3 views (last 30 days)
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.
  6 Comments
Muhammad Usman Saleem
Muhammad Usman Saleem on 3 Feb 2016
if any body tell me what is last line of code doing i shall be thankful to him

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 1 Feb 2016
Edited: Stephen23 on 3 Feb 2016
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!
  29 Comments
Muhammad Usman Saleem
Muhammad Usman Saleem on 15 Feb 2016
Edited: Muhammad Usman Saleem on 16 Feb 2016
@Stephen i want to use interp2 for my interpolation? How can i modify this code in order to do interpolation in 2 dimension?
I unable to get where you edited? code is same as old
Stephen23
Stephen23 on 16 Feb 2016
Edited: Stephen23 on 16 Feb 2016
This is a slightly more complicated task because the interp2 function only accepts "2-D gridded data in meshgrid format". Because of your missing data you do not have gridded data, but scattered data points.
You could resolve this yourself by browsing the MATLAB documentation. Start at the link for interp2 above. You read the very first line and realize that this function will not work (because you do not have gridded data). So you look at the contents on the left-hand side of the page. You click on "Interpolation", because that seems to be the best place to look. Then you see a page with a list of three subcategories: 1-D Interpolation, Gridded Data Interpolation, and Scattered Data Interpolation. Which of these do you think would be best for interpolating your scattered data? Click on the link... there are several functions, and pages of examples.
Try the examples. Think about your task and how to solve it using those examples.
If you get stuck, then ask a new question (with a link to this one) showing how you modified those examples to fit your task.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!