How to average 'data' column of [Latitude, Longitude, Data] data from multiple file and save the average of 'data' column with using the same [Latitude, Longitude]
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi, I have multiple .txt file with consist of column matrix [Latitude, Longitude, Data], where all the Latitude and Longitude is same for all the files. I just want to average all the 'Data' column from each files, and then save it into one new .asc file that consist of [Latitude, Longitude, Data(average)]. I have coding, but I stuck during the average process and could not get the file into [Latitude, Longitude, Data(average)].
clc
clear
format short
outpath = 'C:\Users\mniza\Desktop\SMOS_SSS_CODING\3_DataGridding_SSS_SMOS_Climatology';
[filename,pathName] = uigetfile('*.txt','Select the text-files', 'MultiSelect','on');
filename=cellstr(filename);
C=cell(size(filename));
for k=1:length(filename)
C{k}=textread(fullfile(pathName,filename{k}));
end
average(k)=mean(C(:,3));
if ~exist (outpath)
mkdir (outpath)
end
save('DataAverage.asc', 'average(k)','-ASCII');
0 commentaires
Réponses (1)
Cris LaPierre
le 29 Sep 2023
Is the firle format the same in all your files? If so, I would use a filedatastore to load all the data into a single table, and then groupsummary to compute averages by lat/lon.You can see an example of how to use one to do this in this video from the Data Processing with MATLAB specialization on Coursera.
Here is the final code from that example. You can modify this to work for your data.
myDataStore = fileDatastore("Example*.txt","ReadFcn",@readtable,"UniformRead",true);
data = readall(myDataStore);
data.Properties.VariableNames = ["Lat","Lon","Data"]
LtLnAvg = groupsummary(data,["Lat","Lon"],"mean","Data")
writetable(data,'DataAverage.txt')
This is more a template than a working example, so please adapt the code to work for your data files.
2 commentaires
Cris LaPierre
le 29 Sep 2023
The code is written with the expectation that all the text files you want to import start with "Examples". You will just need to update the format to match your actual file naming convention.
The code is written expecting the files to be in your current folder. You can update that by prepending the folder path to the file name. You can use fullfile for that.
Voir également
Catégories
En savoir plus sur Data Type Conversion dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!