Hi, I want want to read excel files that are located in subfolders in my directory.
6 views (last 30 days)
Show older comments
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!
0 Comments
Accepted Answer
KL
on 7 Dec 2017
Edited: KL
on 7 Dec 2017
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 Comments
KL
on 7 Dec 2017
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.
More Answers (1)
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!