Combine excel files into one

Illustration
Ancalagon8 - 2022-11-14T09:37:40+00:00
Question: Combine excel files into one

I want to run the code below, inside every folder i have an excel file (data.xls) and i want to compare 10 excel files into 1. Can anyone help me?   path = 'D:'; S = dir(fullfile(path, '*', 'data.xls')) for k = 1:numel(S) F = fullfile(S(k).folder,S(k).name); data = readtable(F); d = datetime(strcat(table2array((data(:,1))), {' '}, table2array((data(:,2 ))))); acceleration = table2array((data(:,3 ))); xlswrite('file.xls', data) end  

Expert Answer

Profile picture of Kshitij Singh Kshitij Singh answered . 2025-11-20

so I relicated the same folder tree and file name (same in every folder)
 
this is a slightly modified code , try it :
 
path = 'D:\2019';  
S = dir(fullfile(path, '*', 'data.xls'))

for k = 1:numel(S)
    F = fullfile(S(k).folder,S(k).name);
    data = readtable(F);
    d = strcat(table2array((data(:,1))), {' '}, table2array((data(:,2 ))));
    out = [d data(:,3)]; % date / time / acceleration
    out = renamevars(out,'Var1','Date & Time');
    writetable(out,'file.xls',"Sheet",k)
end

 


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!