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

11 visualizzazioni (ultimi 30 giorni)
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!

Risposta accettata

KL il 7 Dic 2017
Modificato: KL il 7 Dic 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};
for fol_no=1:numel(subfolderNames)
fileInfo = dir(subfolderNames{fol_no});
fileNames = {fileInfo.name};
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)));
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 Commenti
Vlatko Milic
Vlatko Milic il 7 Dic 2017
Thank you for the quick answer.I follow the given answer until the following line:
What is the objective of the above-mentioned line? It is quite similar to the line above (subfolderNames = {subfolderInfo.name}).
Thank you again!
KL il 7 Dic 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.

Accedi per commentare.

Più risposte (1)

Vlatko Milic
Vlatko Milic il 7 Dic 2017
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