average of one column of many text file

I have three hundrad text files and each have 4 columns.I have to do average of 3rd column of all the text files.It will come as a single column.How can we do this.

 Accepted Answer

txtfiles = dir '*.txt' ; % pick txt files in directory
Nfiles = length(txtfiles) ; % total number of files
iwant = zeros(Nfiles,1) ; % initialize the avg array
for i = 1:Nfiles % loop for each file
data = importdata(txtfiles(i).name) ; % load the data
iwant(i) = mean(data(:,3)) ; % get the mean
end

4 Comments

Sir actually by using the programme what I am getting is I have 4808 text files and each text file has 5 column which is rainfall data of 370 days means 370 rows.The output giving is one column that is 4808 rows and it is doing average of that 5 column of indivitual text files and writting 4808 row.I want 370 rows in one column where it do average of 5 th column of 370 rows of each text file.me sending three files.
can this code work for xlsx files?
No, it cannot; it is specifically matlab code

Sign in to comment.

More Answers (0)

Categories

Find more on Agriculture in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!