How to load multiple txt files with many rows and 4 columns and get mean of 3rd and 4th columns
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi I have 400 txt files with many rows, 4 columns and 9 header lines and columns are separated by spaces. How can load them all in matlab and get mean of third and fourth columns.
0 commentaires
Réponse acceptée
Walter Roberson
le 4 Déc 2022
projectdir = '.'; %absolute path would be even better
dinfo = dir( fullfile(projectdir, '*.txt'));
filenames = fullfile({dinfo.folder}, {dinfo.name});
numfiles = length(filenames);
data = cell(numfiles, 1);
for K = 1 : numfiles
thisfile = filenames{K};
thisdata = readmatrix(thisfile, 'HeaderLines', 9);
data{K} = thisdata;
end
After that it depends whether the mean over the columns is intended to be per-file (column3+column4)/2, or per file [mean(column3), mean(column4)], or over all of the files (mean of each element between all of the files.) If you intend mean over all of the files, then you would have to require that the files are all the same size, or else you would have to define what you want to do for the case of files that have fewer rows.
8 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Whos 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!