MATLAB Answers

import multiple excel file

4 views (last 30 days)
Sehoon Chang
Sehoon Chang on 27 Aug 2020
Edited: Stephen on 2 Sep 2020
i am trying to import multiple excel file containing table value to matlab using the following source:
The excel data type varies from file to file (csv., xls., etc.)
using above mentioned link, i have remodelled the code to import tables from each excel file.
csvFiles = dir('*.csv');
numfiles_csv = length(csvFiles)
for a = 1:numfiles_csv
table_csv{a} = readtable(csvFiles(a).name)
end
xlsFiles = dir('*.xls');
numfiles_xls = length(xlsFiles);
for b = 1:numfiles_xls
table_xls{b} = readtable(xlsFiles(b).name);
end
xlsbFiles = dir('*.xlsb');
numfiles_xlsb = length(xlsbFiles);
for c = 1:numfiles_xlsb
table_xlsb{c} = readtable(xlsbFiles(c).name);
end
for each imported table, i wish to display the actual table value. However, the result that i get is as following:
table_csv = 1×1 cell array
{104853×12 table}
How may I change the code to get the result that I am seeking.
And, is there a way to import csv., xls., and xlsb. files in one procedure?
Thank you.

Answers (2)

VBBV
VBBV on 2 Sep 2020
Edited: VBBV on 2 Sep 2020
Type
table_csv{:} table_xls{:} table_xlsb{:}
in the command window to view the contents of cell array
Or you can also use
celldisp(table_csv)
to view the contents of cell array
To import them using one function you can use
Fileid = fopen(filename)
textscan(Fileid,'%s %u ...')
fclose(Fileid);

Stephen
Stephen on 2 Sep 2020
Edited: Stephen on 2 Sep 2020
D = 'absolute or relative path to the folder where the files are saved';
Sc = dir(fullfile(D,'*.csv'));
Sx = dir(fullfile(D,'*.xls'));
Sb = dir(fullfile(D,'*.xlsb'));
S = [Sc(:),Sx(:),Sb(:)];
for k = 1:numel(S)
F = fullfile(D,S(k).name);
S(k).data = readtable(F);
end
Then you can trivially use idnexing to access the data and file information from the same structure, e.g.:
S(1).data % 1st table of data
S(1).name % 1st filename
S(2).data % 2nd table of data
S(2).name % 2nd filename

Community Treasure Hunt

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

Start Hunting!

Translated by