Combine excel files into one

A
Ancalagon8 · Nov 14, 2022 · 2.1K views
Question
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 PhD Expert
Answered Nov 20, 2025
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

 

100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!