Import and process files from different folders in a loop

7 visualizaciones (últimos 30 días)
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 !

Respuesta aceptada

Stephen23
Stephen23 el 6 de Abr. de 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

Más respuestas (0)

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by