Open multiple excel files in loop

8 views (last 30 days)
Ellen
Ellen on 11 Dec 2014
Commented: olahan on 28 Feb 2018
I have about 100 excel files that are all LiFePO4-KAU-3_Cha_01 with consecutive numbers.
I tried setting up a for loop like I saw multiple suggestions of as follows:
for i=03:51
C{i}=xlsread('C:\Documents\Battery\LiFePO4-KAU-3_Cha_%d.xlsx');
end
But it gives me an error and says the file is not found.
I tried some other methods that I found via google also, but I kept getting errors with those too.
Any help on this would be immensely helpful!

Accepted Answer

Sean de Wolski
Sean de Wolski on 11 Dec 2014
Edited: Sean de Wolski on 11 Dec 2014
Here's a similar example that I have:
% Copyright 2014 The MathWorks, Inc.
% Automating File Import
% Select folder containing data interactively
Location = uigetdir;
% Identify where to search for files
% Store the name of all .xls files as a vector D
D = dir([Location, '\*.xlsx']);
% Extract the file names
filenames = {D(:).name}.';
data = cell(length(D),1);
for ii = length(D):-1:1
% Create the full file name and partial filename
fullname = [Location filesep D(ii).name];
% Read in the data
data{ii} = xlsread(fullname);
end
  3 Comments
Sean de Wolski
Sean de Wolski on 11 Dec 2014
Use {} braces. You don't need to store it as a cell, you could use a table or a struct instead. Tables are good for tall columns so that could be a good thing to try. Personally, I use cells for import and then deal with the cell after for converting it to whatever format I need.
olahan
olahan on 28 Feb 2018
Mr. de Wolski. I apologize beforehand for my stupidity, but I am encountering a similar problem. I wanted to do something similar, so I found this thread and I hope you do not mind that I ask a question here.
I used your code written above and it works brilliantly; however, I want to pull data out from the data cell using a for-loop. All my excel files are configured the same way, so I would like to, for example, pull out the data for cell 5,2 and the range 1:11,4 in data(1), data(2), et cetera.
I guess you already answered this question with your comment about {} braces, but I am a bit lost about the correct syntax.
Thank you.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!