finding average from 20 text file data based on the matched row from every single text file

Hi everyone. i would like to ask some question. i have 20 text file data which contains only numbers with [r,c]= [61441,27]. what i need to do is to calculate their average from 20 text file column by column. since the file is quite big so it has problem in calculating using excel. So i would like to used matlab coding but the coding doesnt work like i want.
pathToFolder = '.';
Directory=fullfile('C:','Users','ASUS','Desktop','aini','eeglab11_0_0_0b','ALL EEG DATA','2. PROCESSED','.TEXT FILE','BY SESSION','SESSION 1','B1');
files = dir(fullfile(Directory,'*.txt','B%d'));
% Names={files.name};
%matrix initialization
files1= dlmread(fullfile(Directory,files{1}));
[r,c] = size(files1);
for i = 2:numel(Names)
files= dlmread(fullfile(Directory,Names{i}));
[nrows, ncols] = size(files);
end
dataMean = mean(files(nrows, ncols));
dlmwrite('B1_ave.txt', dataMean);

Answers (1)

files = dir(fullfile(Directory,'*.txt','B%d'));
...
for i = 2:numel(Names)
files= dlmread(fullfile(Directory,Names{i}));
...
You've just overwritten the array of file names returned by dir that you want to process with the values of the read file. Do something more like--
files = dir(fullfile(Directory,'*.txt','B%d'));
data= dlmread(fullfile(Directory,files{1}.name)); % read first file
[r,c] = size(data); % good thought; get the data size
nFile=length(files); % also for the number
means=zeros(nFile,c); % preallocate for results
means(1,:)=mean(data); % first file means by column
for i=2:nFile % now the rest follow
means(i,:)=mean(dlmread(fullfile(Directory,files(i).name))); % add each new mean
end
dataMean = mean(means); % and the grand mean since mean(mean(data))=mean(allData)
dlmwrite('B1_ave.txt', dataMean);
One can also build the dataMean by taking
dataMean=mean([dataMean;mean(dlmread(fullfile(Directory,files(i).name)))]);
thus doing away with the temporary means array but I wrote it as above to make it easier to follow what are doing.

Asked:

on 11 Feb 2015

Edited:

dpb
on 11 Feb 2015

Community Treasure Hunt

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

Start Hunting!