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)
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.

Réponse acceptée

Walter Roberson
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
SK
SK le 5 Déc 2022
Yes I checked the the file order is the same as the increasing time.

Connectez-vous pour commenter.

Plus de réponses (0)

Tags

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by