Import and process files from different folders in a loop

10 vues (au cours des 30 derniers jours)
Loriann Chevalier
Loriann Chevalier le 6 Avr 2022
Hello everyone,
I would like to import several files in Matlab with a for loop. There are many folders containing my files with a similar directory but one part that is different, like this
/Users/Someone/Desktop/BlaBla/Model_Paris/Output
/Users/Someone/Desktop/BlaBla/Model_NewYork/Output
/Users/Someone/Desktop/BlaBla/Model_London/Output
etc.
I would like to import all the files (which are .txt files) in the Output folders (and later on process them) with a loop to avoid copy-pasting the same line many times. Does anyone have an idea how to do so ?
Thanks !

Réponse acceptée

Stephen23
Stephen23 le 6 Avr 2022
S = dir('C:/Users/Someone/Desktop/BlaBla/**/Output/file.txt');
for k = 1:numel(S)
F = fullfile(S(k).folder,S(k).name);
S(k).data = readtable(F); % or whatever function works best for your file
end
  1 commentaire
Loriann Chevalier
Loriann Chevalier le 7 Avr 2022
This is exactly what I was looking for, thank you very much !

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur File Operations dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by