Hey,
I'd like to be able to give a program a directory, and then have it run a second program on all files in that directory. For example, say I have a function, myFun(filePath) that reads, processes, and displays data from the file specified by filePath. Now, I'd like to write another function, in pseudocode below:
funtion dirFun(dirPath)
f = files(dirPath);
for x = 1:length(files);
myFun(f[x]);
end
Something like this. I want a function, here "files" which will return all of the file names within the directory, so that I can run the program on all my files. Is there such a function in MATLAB?

 채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 8월 11일

13 개 추천

The function is dir().
Files=dir('*.*');
for k=1:length(Files)
FileNames=Files(k).name
end

댓글 수: 4

Shabbir ali
Shabbir ali 2019년 3월 28일
Thanks! It works perfectly. This is exactly what I was looking for.
Amin S
Amin S 2020년 5월 7일
Excuse me, I use this code but it just save the file name of one of the files, not all of them. How can I save all file names within the folder using a loop?
Stephen23
Stephen23 2020년 5월 7일
편집: Stephen23 2020년 5월 7일
@Amin S: no need for a loop:
S = dir(...); % all of the names are in the structure S anyway.
N = {S.name}; % but if you really want a cell array of names, do this.
Walter Roberson
Walter Roberson 2022년 7월 10일
편집: Walter Roberson 2022년 7월 10일
What leads you to expect that nearly 11 years ago, @Clement Wong happened to need to start from the third entry instead of the first?
I suspect that you are thinking that the first entry is "." and the second entry is ".." but if so then that is an incorrect assumption on every operating system that MATLAB has ever been supported on. If you want to filter out "." and ".." you should do so by name, not by assuming they are in a particular position.

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

추가 답변 (1개)

Andrii Mazur
Andrii Mazur 2018년 6월 16일

1 개 추천

Thank you!

카테고리

도움말 센터File Exchange에서 File Operations에 대해 자세히 알아보기

질문:

2011년 8월 10일

편집:

2022년 7월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by