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

7 次查看(过去 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!

Community Treasure Hunt

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

Start Hunting!

Translated by