import many txt files

Hi, I have many text files about 16574, I need to import them to matlab and calculate the average of each files.

2 comentarios

Jan
Jan el 22 de Feb. de 2017
What is your question? What have you tried so far?
dbmn
dbmn el 22 de Feb. de 2017
I suggest using the following functions.
1. Files=dir(fullfile(pwd,'*.txt'));
2. for j=1:1:numel(Files)
3. textread / fscanf / csvread
4. mean()
and if you get stuck, ask again :)

Iniciar sesión para comentar.

Respuestas (2)

dpb
dpb el 22 de Feb. de 2017

0 votos

May find following of interest... Filefun: Apply a function to files

2 comentarios

malihe keykhai
malihe keykhai el 23 de Feb. de 2017
thanks for your help. I couldn't solve my problem. I write your answer in this way : Files=dir(fullfile(pwd,'*.txt')); for j=1:1:numel(Files) csvread('Files') mean(Files) end but it doesn't work. I don't know how can export mean of each file in one text files.
Jan
Jan el 23 de Feb. de 2017
@malihe keykhai: Thanks for posting, what you have done so far. The code contains some problems: "csvread('Files')" tries to read the file names 'Files'. mean(Files) tries to calculate the average of the struct Files created by the dir() command, and this must fail. See my answer.

Iniciar sesión para comentar.

Jan
Jan el 23 de Feb. de 2017

0 votos

Folder = pwd:
Files = dir(fullfile(Folder, '*.txt'));
Result = cell(1, numel(Files));
for j = 1:numel(Files)
aFile = fullfile(Folder, Files(j).name);
Data = csvread(aFile);
Result{j} = mean(Data, 1);
end
The question remains, what "average of each files" exactly mean. Here the mean() along the first dimension is calculated and stored in a cell. But perhaps you want something else.

2 comentarios

malihe keykhai
malihe keykhai el 23 de Feb. de 2017
Thanks it works. each file contain temperature for long time and I need average of them. Would you please guide me, I want result, each cell which fill by average of file show me related to which file. Thanks
Jan
Jan el 23 de Feb. de 2017
I do not understand your question. My current code creates a cell, which contains the vector with the mean of each file: Result{k} contains the mean of Files(k).name. Is this what you need?

Iniciar sesión para comentar.

Preguntada:

el 22 de Feb. de 2017

Comentada:

Jan
el 23 de Feb. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by