MATLAB Answers

How can I recursively process files in subdirectories using MATLAB?

조회 수: 128(최근 30일)
How can I recursively process files in subdirectories using MATLAB?
I have files located in multiple subdirectories within a directory. I would like to be able to process the data in each subdirectory usng the same functions. How can I do this without having to manually run the code in each directory?

채택된 답변

MathWorks Support Team
Processing the files in a directory hierarchy can be accomplished through the use of recursive functions. The general format is:
1) Call the recursive function with the name of a directory.
2) Use the DIR function to obtain a listing of the directory.
3) Loop through the entries in the listing.
4) If an entry is a directory, then call the function recursively, passing the subdirectory's name as the directory to process.
5) If an entry is not a directory, then process the file.
Please find an example file in the MATLAB Central submission SUBDIR:
Note that MathWorks does not guarantee or warrant the use or content of these submissions. Any questions, issues, or complaints should be directed to the contributing author.

  댓글 수: 0

댓글을 달려면 로그인하십시오.

추가 답변(1개)

lvn
lvn 11 Mar 2020
It is high time Matlab updates some of the official answers, as many are outdated.
This is an example, as of R2016b, no need for external tools:
files=dir('C:\temp\**\*.txt');
for k=1:length(files)
fn=fullfile(files(k).folder, files(k).name);
%process the file fn here
end

  댓글 수: 0

댓글을 달려면 로그인하십시오.

제품


릴리스

R13SP1

Community Treasure Hunt

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

Start Hunting!

Translated by