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 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Cris LaPierre
2023-9-29
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 个评论
Cris LaPierre
2023-9-29
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!