MATLAB Answers


import multiple spreadsheats at once (.xlsx files)

Asked by Joey Smit on 12 May 2017
Latest activity Commented on by Joey Smit on 13 May 2017
so I have to import multiple spreadsheats at once (roughly 400). I can import them all manually like
Is there a faster way to do this? The spreadsheats are all 2D matrices. thanks in advance


Sign in to comment.

1 Answer

Answer by Joseph Cheng
on 13 May 2017
Edited by Joseph Cheng
on 13 May 2017

well first you'll want to get away from generating variables in that fashion as you'll basically have A400 or more which is hard to keep track of. instead of manually you can use the function dir() which can poll a file for the files in a folder. (in combination with uigetdir/uigetfile you can navigate to another folder without coding in the file locations.)
xlfiles = dir('*.xlsx')
for ind = 1:numel(xlfiles)
A(:,:,ind) = xlsread(xlfiles(ind).name);
if they are at different sizes you can use structs which i think goes like this:
data = struct('A',[])
for ind = 1:numel(xlfiles)
data(ind).A = xlsread(xlfiles(ind).name);
or alternatively put it into a cell array

  1 Comment

Ok, I get the idea but what does the .name thing do in the 3rd line? I can't find any example of it. I've been able to import some of the xlsx files manually using the same line of code (more or less), but I could skip the .name stuff there.

Sign in to comment.