read and write xlsx files

8 views (last 30 days)
Matin jaberi
Matin jaberi on 5 Oct 2022
Commented: Mathieu NOE on 24 Oct 2022
Hi All,
I have a folder with 20 xlsx files. i want matlab to read all the xlsx files and take column 3 from each file and write in a new table. (please note i want column 3 of each xlsx files to be in a sepeate column in the new table so i can plot them against each other.

Answers (1)

Mathieu NOE
Mathieu NOE on 5 Oct 2022
hello see example code below
you can either write the result as a simple numeric array (as here) with writematrix, but if you really need the result as a table, simply use writetable instead.
I assuled here all input data files have same amount of data (number of rows) so it was pretty easy to do thevertical concatenation. Otherwise we might have to do some padding
fileDir = pwd; % current directory (or specify which one is the working directory)
outfile = 'OUT.xlsx'; % output file name
S = dir(fullfile(fileDir,'*.xlsx')); % get list of data files in directory
S = natsortfiles(S); % sort file names into natural order , see :
out_data = [];
for k = 1:length(S)
filename = S(k).name % to actually show filenames are sorted (see command window)
tmp = readmatrix( fullfile(fileDir, filename)); %
out_data = [out_data tmp(:,3)]; % store column 3 of each xlsx files and do vertical concatenation
% store out_data in excel file
Mathieu NOE
Mathieu NOE on 24 Oct 2022
if my answer has helped you, do you mind accepting it ?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!