How to load multiple txt files with many rows and 4 columns and get mean of 3rd and 4th columns

2 views (last 30 days)
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.

Accepted Answer

Walter Roberson
Walter Roberson on 4 Dec 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 Comments

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!