Average of data multiples excel sheets
Show older comments
I have one excel file that contains 40 sheets with two column dataseries in each of the sheets. I need a matlab codes that can average the data for the sheet1 - sheet 10, sheets 11 - sheet 20, sheet 21-sheet30, sheet31-sheet40 separately and write the results in sheet 41, sheet 42, sheet 43 and sheet 44 respetively. Can anyone help. Thanks.
Olusola
2 Comments
Mathieu NOE
on 6 Oct 2021
hi
can you share a representative excel file ?
tx
Ojo Olusola
on 6 Oct 2021
Answers (1)
Mathieu NOE
on 7 Oct 2021
hello
so this is my suggestion
as we have here 10 sheets , my demo works for either groupped 2 or 5 sheets
this is driven by the parameter k , and of course it must be choosen accordingly to the number of input sheets
with k = 5 , 2 new sheets are added with the averaged data of sheets 1 to 5 and 6 to 10
with k = 2 , 5 new sheets are added with the averaged data of sheets 1 to 2 , 3 to 4 ,..., 9 to 10
code :
clc
clearvars
% Importing Data from excel across multiple sheets.
filename = 'mydata2a.xlsx';
[~,sheet_name]=xlsfinfo(filename);
nsheets = numel(sheet_name);
% retrieve raw data
for k=1:nsheets % nsheets
[data,~,~]=xlsread(filename,sheet_name{k});
% mean values per sheet
data_mean_one_sheet(k,:) = mean(data);
end
%% export
% mean of k successive sheets among nsheets (total)
k = 5;
for ck = 1:nsheets/k
ind = (1:k)+(ck-1)*k;
data_mean_k_sheets(ck,:) = mean(data_mean_one_sheet(ind,:));
xlswrite(filename,data_mean_k_sheets(ck,:),nsheets+ck);
end
13 Comments
Ojo Olusola
on 7 Oct 2021
Mathieu NOE
on 7 Oct 2021
ok , got it !
I think this is the correct answer !!
clc
clearvars
% Importing Data from excel across multiple sheets.
filename = 'mydata2a.xlsx';
[~,sheet_name]=xlsfinfo(filename);
nsheets = numel(sheet_name);
% retrieve raw data
for k=1:nsheets % nsheets
T = readtable(filename,"Sheet",sheet_name{k});
time = T.Time;
DF(:,k) = T.DF;
DR(:,k) = T.DR;
end
%% export
% mean of k successive sheets among nsheets (total)
k = 5;
for ck = 1:nsheets/k
ind = (1:k)+(ck-1)*k;
DF_avg = mean(DF(:,ind),2); % mean along vertical direction
DR_avg = mean(DR(:,ind),2); % mean along vertical direction
out_table = table(time,DF_avg,DR_avg);
writetable(out_table,filename,"Sheet",nsheets+ck);
end
Ojo Olusola
on 8 Oct 2021
Mathieu NOE
on 11 Oct 2021
hello
oh, I found the reason why; when I add the two extra sheets , my time variable is with lower case t whereas your original sheets have Time with upper case T.
also my original code woud read the two extra sheets after a first iteration, as we have now more sheets
tht's why I also now I forced nsheets = 10 to be sure the 2 extra sheets are not taken into account in the read process - if you run the script multiple times on the same excel file.
code updated :
clc
clearvars
% Importing Data from excel across multiple sheets.
filename = 'mydata2a.xlsx';
[~,sheet_name]=xlsfinfo(filename);
% nsheets = numel(sheet_name);
nsheets = 10;
% retrieve raw data
for k=1:nsheets % nsheets
T = readtable(filename,"Sheet",sheet_name{k});
Time = T.Time;
DF(:,k) = T.DF;
DR(:,k) = T.DR;
end
%% export
% mean of k successive sheets among nsheets (total)
k = 5;
for ck = 1:nsheets/k
ind = (1:k)+(ck-1)*k;
DF_avg = mean(DF(:,ind),2); % mean along vertical direction
DR_avg = mean(DR(:,ind),2); % mean along vertical direction
out_table = table(Time,DF_avg,DR_avg);
writetable(out_table,filename,"Sheet",nsheets+ck);
end
Mathieu NOE
on 12 Oct 2021
Hi
if my contribution has helped you, do you mind accepting it ?
tx
Mathieu NOE
on 25 Oct 2021
hello again
problem solved ?
Ojo Olusola
on 25 Oct 2021
Mathieu NOE
on 25 Oct 2021
well
maybe my comments of the code was unclear but the latest code I sent you is doing what your looking for
dimensions DF_avg 14974x1
dimensions DR_avg 14974x1
as you ask
they are average of 5 sheets , keeping the original vertical length of 14974
now you excel file has two more sheets with 14974x2 of data
check it again !
example first additionnal sheet :

clc
clearvars
% Importing Data from excel across multiple sheets.
filename = 'mydata2a.xlsx';
[~,sheet_name]=xlsfinfo(filename);
% nsheets = numel(sheet_name);
nsheets = 10;
% retrieve raw data
for k=1:nsheets % nsheets
T = readtable(filename,"Sheet",sheet_name{k});
Time = T.Time;
DF(:,k) = T.DF;
DR(:,k) = T.DR;
end
%% export
% mean of k successive sheets among nsheets (total)
k = 5;
for ck = 1:nsheets/k
ind = (1:k)+(ck-1)*k;
DF_avg = mean(DF(:,ind),2); % mean of k successive sheets
DR_avg = mean(DR(:,ind),2); % mean of k successive sheets
out_table = table(Time,DF_avg,DR_avg);
writetable(out_table,filename,"Sheet",nsheets+ck);
end
Ojo Olusola
on 25 Oct 2021
Mathieu NOE
on 26 Oct 2021
hello
glad we finally make it work ! if my contribution has helped you, do you mind accepting it ?
-----------------------------------------------------------------------
For ANFIS model, Genetic Algorithm (GA) model, RBF : I personnally have not make any code or application in that area but there are quite a lot of available ressources and examples on internet / youtube / FEX matlab :
Mathieu NOE
on 5 Nov 2021
hello
do you mind accepting my answer ?
tx
Ojo Olusola
on 5 Nov 2021
Mathieu NOE
on 19 Nov 2021
hello again
sorry , but it seems you didn't press the "accept" button ...
all the best
Categories
Find more on Text Data Preparation 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!