Calculate mean of row for multiple files

Hello,
I have a problem with a code. First of all, I have multiple .txt files, and each one has 30 rows and 1 column.
I would like to calculate the mean of each row of all .txt files ( I mean I would like to calculate the mean of the first row of each .txt files, after the mean of the second row of each .txt files etc). After calculating the mean of all the .txt files, I woulf like to create a final .txt file which will contains the mean of each row of each .txt file
(I mean one .txt file with 30 rows and one column)..
Could anyone help me?

Réponses (1)

Adam Danz
Adam Danz le 5 Juin 2020
Modifié(e) : Adam Danz le 8 Juin 2020

0 votes

" I have multiple .txt files, and each one has 30 rows and 1 column. "
First you need the paths or the file names to each text file. If you don't have a variable that stores this list, you can use dir() to list the content of directories and then you can sort out which files to read. There are lots of answers in this forum showing how to do that.
Then use readmatrix() to read in each text file within a loop. Since the files are not large, you can store them all in an array.
"I would like to calculate the mean of each row of all .txt files"
Your question states that the text files are "each one has 30 rows and 1 column" so what do you mean by calculating the mean of each row? By that definition, each row only has 1 value. Assuming the description is a mistake, once you read the data in you can use mean(A,2) to compute the mean across rows of "A".
If you have any questions or problems, leave a comment including your updated code and I'd be happy to get you unstuck.

4 commentaires

Hi Adam,
I have kinda the same problem.
I have many files with an".IV0" ending (this is something similiar to .txt). In each file I have an header of 15 rows. This header should not be aken into account. After the header there are 3 columns (seperated by " " and about 81 rows). What I want to do now is to average e.g. row "16" column "1" of each .IV0 file and make an outputfile, which has 3 columns and about 81 rows and in each (row, column) is the average of all the same (row, column) elements from all the different files.
I have tried what you have said, but don´t really know what to do anymore...Can you help me? This is the code I tried:
folder = 'C:\Users\simon\Averager\';
IV0Files = dir(fullfile(folder, '*.IV0'));
readmatrix(IV0Files);
mean(A,2);
Image Analyst
Image Analyst le 10 Déc 2020
Simon, see the FAQ:
Inside the loop you need to assign the output of readmatrix to A, and add that A to an overall Asum". Then after the loop you need to divide Asum by the number of files and assign that mean matrix to a variable. Otherwise there will be no A by the time you call mean(), and if there were, you wouldn't have stored the mean value in any other variable.
If you still can't figure it out, post your code, and at least 2 data files in a new question, after reading this:
Simon Keegan
Simon Keegan le 10 Déc 2020
Hi, actually I think that you are probably right, but I don´t undertand a wörd. It is like talking chinese. I am totally stuck. THe only thiing I can do ist to understand existing codes, but to create codes I guess I am just too bad in coding...

Connectez-vous pour commenter.

Catégories

En savoir plus sur File Operations dans Centre d'aide et File Exchange

Question posée :

le 5 Juin 2020

Commenté :

le 11 Déc 2020

Community Treasure Hunt

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

Start Hunting!

Translated by