Problem in adding column in text file
1 次查看(过去 30 天)
显示 更早的评论
Hello everyone,
I have an hdf file containing lat, lon and another parameter hp. I also have a text file that contains lat lon values along with other variables. What I want to do is read the lat lon values from text file, match/collocate them with the lat lon of hdf and extract the hp values at corresponding points (means extract the hp at lat lon of hdf). And then place these hp values in the form of column in a text file (in the same text file).
I have tried to do this in the following way but it didn't add the column in the text file. My first question is
1) Is there a way of doing this through for loop?
2) Secondly, how to add column in the text file?
Thank you.
%% Read latitude longitude from hdf file
M_CP = hdfread('D:\MYD06_L2.A2017198.0540.061.2018034074555.hdf', 'Cloud');
M_lat = hdfread('D:\MYD06_L2.A2017198.0540.061.2018034074555.hdf', 'Latitude');
M_lon = hdfread('D:\MYD06_L2.A2017198.0540.061.2018034074555.hdf', 'Longitude');
lat_txt = tab.lat;
lon_txt = tab.lon;
lat_i = imresize(M_lat,[7570 270],'bilinear');
lon_i = imresize(M_lon,[7570 270],'bilinear');
%% To match lat lon values in text file with hdf file
mask = ismembertol(lattxt, lat_i) & ismembertol(lontxt, lon_i);
mask2 = M_CP(mask);
%% Add column in the text file
filename = 'D:\Data.txt';
Data = importdata(filename);
dlmwrite('D:\TestData', [Data mask2], '%f\n');
2 个评论
采纳的回答
Walter Roberson
2021-9-9
Note that you need to look in the new file for the new column, not the old one.
%% Read latitude longitude from hdf file
if ispc()
hdf_filename = 'D:\MYD06_L2.A2017198.0540.061.2018034074555.hdf';
data_filename = 'D:\Data.txt';
new_filename = 'D:\TestData.txt';
M_CP = hdfread(hdf_filename', 'Cloud_Phase'); % 2030X1354 double
M_lat = hdfread(hdf_filename, 'Latitude'); % 406x270 double
M_lon = hdfread(hdf_filename, 'Longitude'); % 406x270 double
else
hdf_filename = 'Hdf_Data.xlsx';
data_filename = 'Data.txt';
new_filename = 'TestData.txt';
M_CP = readmatrix(hdf_filename, 'sheet', 'Phase');
M_lat = readmatrix(hdf_filename, 'sheet','Latitude');
M_lon = readmatrix(hdf_filename, 'sheet', 'Longitude');
end
%% Read lat lon from Text file (I imported data in the form of table (7570x28 table)).
tab = readtable(data_filename, 'ReadVariableNames', true, 'VariableNamingRule','preserve');
lat_txt = tab.lat; % extract the column of latitude values
lon_txt = tab.lon; % extract the column of longitude values
F = scatteredInterpolant(M_lat(:), M_lon(:), M_CP(:));
mask2 = F(lat_txt, lon_txt);
%% Add column in the text file
tab.mask2 = mask2;
writetable(tab, new_filename);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 HDF5 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!