Problem in adding column in text file

1 visualización (últimos 30 días)
Zhou Ci
Zhou Ci el 8 de Sept. de 2021
Comentada: Zhou Ci el 11 de Sept. de 2021
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 comentarios
Walter Roberson
Walter Roberson el 8 de Sept. de 2021
What error did you encounter?
We do not have that hdf file to test with.
Zhou Ci
Zhou Ci el 8 de Sept. de 2021
Actually it doesn't show any error.
After running the code, when I checked the text file, it was same (means column was not added to it).
I have attached csv file containing latitude, longitude and phase values from hdf file.

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 9 de Sept. de 2021
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);
  1 comentario
Zhou Ci
Zhou Ci el 11 de Sept. de 2021
It gives me error at this line:
Error using scatteredInterpolant
The number of data point locations should equal the number of data point values.

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by