Open multiple excel files in loop

조회 수: 3 (최근 30일)
Ellen
Ellen 2014년 12월 11일
댓글: olahan 2018년 2월 28일
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!

채택된 답변

Sean de Wolski
Sean de Wolski 2014년 12월 11일
편집: Sean de Wolski 2014년 12월 11일
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
Sean de Wolski
Sean de Wolski 2014년 12월 11일
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 2018년 2월 28일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by