scatter data interpolation from text files in matlab

4 次查看(过去 30 天)
I am going to use scatteredInterpolant for interpolation of missing data. My data consist on text files name from Output_00 to Output_23 in a folder. Each text file consist on three columns, first is latitude, second is longitude and third is temperature. -9999.0000 value in temperature column representing NaN or missing data. This value appear randomly (some times appear in start, middle or bottom in file.
I want to make a code which read each text file from the folder and using scatteredInterpolant, interpolate 3rd column of each text file and then save this interpolated text file with output_interpol_00.text. It means there will be 24 more text files which will be interpolated
I have prepared this code. But it is manually entry requirement which is time consuming.I want to use looping or other to energize my code and instead on manully it run for all 24 text files in folder
% Load the data
data = load('Output_00.txt');
% separate the data columns, just to make the code clear
Lat = data(:,1); % Column 1 is Latitude
Lon = data(:,2); % Column 2 is Longitude
Tmp = data(:,3); % Column 3 is Temperature
% Find the "good" data points
good_temp = find(Tmp > -9999.000);
% creating vector
T = scatteredInterpolant(Lat(good_temp),Lon(good_temp),Tmp(good_temp),'linear');
% find the "bad" data points
bad_temp = find(Tmp == -9999.000);
% use the interpolation object to interpolate temperature values
interp_values = T(Lat(bad_temp),Lon(bad_temp));
% replace the bad values with the interpolated values
Tmp(bad_temp) = interp_values;
interpolant_output=[Lat';Lon';Tmp']';
fid = fopen('interpot_linear_00.txt','wt');
for ii = 1:size(interpolant_output,1)
fprintf(fid,'%.3f\t',interpolant_output(ii,:));
fprintf(fid,'\n');
end
fclose(fid)
Please also check it whether it is correct or not??
I have attached one text file of my data set. Please check it
Thanks for this kind assistance
  3 个评论
Walter Roberson
Walter Roberson 2016-3-2
What assistance are you looking for at this time? The major question you seemed to have was about processing the files automatically, and the code you posted here does that.
Muhammad Usman Saleem
Dear Walter;
there are two codes i have shared in this question
  1. In first code, i have tried my effort in order to processes on the single text file. It is very time consuming to write each line for each text file(24 in no) also when i want to implement every new technique i have to again write this whole code? It will be more keen work and time consuming I want to make time saving code, which read all text file and interpolate their 3rd column -9999.000 value as mentioned in my code. Then save each text file with this format inter_method_00.text. while method stand for method and 00 stand for output_oo.text file
  2. Look the same idea i above mentioned is done by @Stephon, but this code runing perfrectly for Interpolate1 not for scatteredInterpolant there is just little modification in my second code for my requirements. Please read my question and give an assistance for my requirements.
Thanks in advance for this kind assistance

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2016-3-3
编辑:Walter Roberson 2016-3-3
S = dir('Output_*.txt');
N = sort({S.name});
for K = 1 : length(N)
infile = N{K};
whichfile = sscanf(infile, 'Output_%c%c');
outfile = ['interpot_linear_' whichfile '.txt'];
% Load the data
data = load(infile);
% separate the data columns, just to make the code clear
Lat = data(:,1); % Column 1 is Latitude
Lon = data(:,2); % Column 2 is Longitude
Tmp = data(:,3); % Column 3 is Temperature
% Find the "good" data points
good_temp = find(Tmp > -9999.000);
% creating vector
T = scatteredInterpolant(Lat(good_temp),Lon(good_temp),Tmp(good_temp),'linear');
% find the "bad" data points
bad_temp = find(Tmp == -9999.000);
% use the interpolation object to interpolate temperature values
interp_values = T(Lat(bad_temp),Lon(bad_temp));
% replace the bad values with the interpolated values
Tmp(bad_temp) = interp_values;
interpolant_output=[Lat';Lon';Tmp']';
fid = fopen(outfile,'wt');
for ii = 1:size(interpolant_output,1)
fprintf(fid,'%.3f\t',interpolant_output(ii,:));
fprintf(fid,'\n');
end
fclose(fid)
end
  18 个评论
Walter Roberson
Walter Roberson 2016-3-11
I am not sure what you are trying to say there.
scatteredInterpolant does not invoke those kriging or IDW tools, and is does not happen to be customizable to invoke them, so you would need to alter your code to invoke the external tools.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Language Support 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by