Can I import multiple sheets from one excel file using for loop?

8 views (last 30 days)
Hi everyone,
I am currently trying to import multiple sheets from one excel file by coding a for-loop.
As the number of sheets within the source excel file might vary in the future, I am trying to consider this in my coding.
At the current state of the code shown below, the output table "T" only contains the imported values from the last sheet of the source excel file.
Is it possible to get different individual output tables equal to the number of sheets within the original excel file for further processing in MATLAB?
Thanks for your support.
sheets = sheetnames("Notenlisten_TEST.xlsx"); % identifying no. of sheets in single excel file
nsheets = numel(sheets); % to identify number of iterations
% Data Import
for iSheetData = 1:nsheets
T = readtable("Notenlisten_TEST.xlsx","Sheet",iSheetData);
end

Accepted Answer

Stephen23
Stephen23 on 3 Mar 2022
Edited: Stephen23 on 3 Mar 2022
"Is it possible to get different individual output tables equal to the number of sheets within the original excel file for further processing in MATLAB? "
Of course, just store them in a container array, for example in a cell array:
F = "Notenlisten_TEST.xlsx";
S = sheetnames(F);
N = numel(S);
C = cell(1,N);
for k = 1:N
C{k} = readtable(F,"Sheet",S(k));
end
See also:

More Answers (0)

Categories

Find more on Data Import from MATLAB in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!