Import and process files from different folders in a loop

10 次查看(过去 30 天)
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 !

采纳的回答

Stephen23
Stephen23 2022-4-6
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by