How to load multiple txt files with many rows and 4 columns and get mean of 3rd and 4th columns
1 view (last 30 days)
Show older comments
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 Comments
Accepted Answer
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
More Answers (0)
See Also
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!