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!