Hi, I want want to read excel files that are located in subfolders in my directory.

조회 수: 9 (최근 30일)
Hi, I want want to read excel files that are located in subfolders in my directory. More specifically, I have different street names that are located in a folder named "Street names". In this folder I have circa 100 various folders, each folder containing an excel file with one sheet. The excel files contain hourly based energy data during three years. Moreover, I want to sum the energy use on a yearly basis. See attached file for snap picture of an example of the search path. Note that the directory is located in Matlab_signatur.
Thank you!

채택된 답변

KL
KL 2017년 12월 7일
편집: KL 2017년 12월 7일
Importing data from many files has been explained exhaustively. Please read these:
As you would notice, using dir is the basic idea here. For your case, you'll have to use it like,
path1 = '...';
subfolderInfo = dir(path1);
subfolderNames = {subfolderInfo.name};
subfolderNames(ismember(subfolderNames,{'.','..'}))=[];
for fol_no=1:numel(subfolderNames)
fileInfo = dir(subfolderNames{fol_no});
fileNames = {fileInfo.name};
fileNames(ismember(fileNames,{'.','..'}))=[];
for file_no = 1:numel(fileNames)
%modify the way you want to store data
ImportedData{fol_no,fileNo} = readtable(fullfile(fileInfo.path,fileNames(file_no)));
end
end
this is just an example. Pre-allocating your variable would speed up the process but for that you should know how many files you are importing. I'd keep all my files in one folder (movefile) and then make this import process simpler.
  댓글 수: 2
Vlatko Milic
Vlatko Milic 2017년 12월 7일
Thank you for the quick answer.I follow the given answer until the following line:
subfolderNames(ismember(subfolderNames,{'.','..'}))=[]
What is the objective of the above-mentioned line? It is quite similar to the line above (subfolderNames = {subfolderInfo.name}).
Thank you again!
KL
KL 2017년 12월 7일
when you use dir, apart from the folder names you also get '.' and '..', so with the above command, you remove those two cells as they are interesting to extract files.
Just execute the code line by line without the semicolon and see what each line does.

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

추가 답변 (1개)

Vlatko Milic
Vlatko Milic 2017년 12월 7일
Yes, now I see. Thank you again!

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by