MATLAB: Scatter data interpolation from text files in matlab

interpolationinterpolation2MATLABscatteredinterpolanttext file

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

Best Answer

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
Related Question