How to loop through a folder?

246 次查看(过去 30 天)
andrew
andrew 2014-3-11
I currently have 24 folders within a folder and I was wondering if it was possible to loop through the 24 folders and extract information from a subfolder within the 24 folders.
  1 个评论
Brando Miranda
Brando Miranda 2018-3-30
great question. This is some of the trivialest things to do in a language and its incomprehensible how hard it is to do in matlab.

请先登录,再进行评论。

回答(3 个)

Image Analyst
Image Analyst 2014-3-11
See my attached demo where it recurses through a folder and all subfolders.
  13 个评论
Rik
Rik 2020-12-10
You can use the ** option of dir. What did you try?
Image Analyst
Image Analyst 2020-12-10
Yes, essentially it's
filePattern = sprintf('%s/**/*.*', topLevelFolder);
allFileInfo = dir(filePattern);
See my attached m-file for a full demo with tons of explanations.

请先登录,再进行评论。


Sean de Wolski
Sean de Wolski 2014-3-11
编辑:Sean de Wolski 2014-3-11
You can use the dir command to give you the names and then loop over them.
files = dir;
directoryNames = {files([files.isdir]).name};
directoryNames = directoryNames(~ismember(directoryNames,{'.','..'}))
Now run the for-loop over directoryNames and apply your analysis to each
  7 个评论
Brando Miranda
Brando Miranda 2018-3-30
why does
directoryNames = {files([files.isdir]).name};
work? its really cryptic? are u using list comprehensions or what dark magic is going on there?
Stephen23
Stephen23 2018-3-30
编辑:Stephen23 2018-3-30
@Brando Miranda: "list comprehensions" exist only in Python. They do not exist in MATLAB.
The syntax in both your comments uses comma-separated lists. Comma-separated lists are very simple:

请先登录,再进行评论。


manideep reddy
manideep reddy 2018-4-18

see my code below but make sure that the folder which we are looping through contains only flolders and not files.

 cd pwd ; ## Or any other working directory you want
X = ls  ;
disp(X) ;
for itr = 1:size(X)(1)
	string_1 = X(itr,:) ;
	string_2{itr} = deblank(string_1) ;  ## This deletes the trailing blank spaces 			
end
for itr = 1:size(X)(1)
	cd(string_2{itr})
	cd ../
	# DO SOMETHING #
end
  end
  2 个评论
Stephen23
Stephen23 2018-4-18
编辑:Stephen23 2018-4-18
This code has several bugs and could be significantly improved:
  • Do NOT use cd. Using cd is slower than using absolute/relative filepaths, makes debugging more difficult, and changes which functions/scripts are accessible to MATLAB. It is NOT necessary to cd directories where datafiles are stored because all MATLAB filereading/writing functions accept relative/absolute paths. Basically cd should only be used interactively, and NOT used in code. See:
  • Do NOT use ls for parsing filenames. This operator is intended for displaying a list in the command window, but parsing its output char array is a waste of time. dir is what the MATLAB documentation recommends and shows in all examples that require reading multiple files from a folder, e.g.:
  • size(X)(1) is not valid MATLAB syntax, and will throw an error.
  • string_2 is not preallocated.
  • A loop is anyway not required for deblanking the filenames: simply use deblank(cellstr(X)).
Rather than following this buggy code, I recommend that other users follow the examples shown in the MATLAB help and wiki:
manideep reddy
manideep reddy 2018-4-20
Thank you very much. I am actually not a computer science student. So, just answered the query without considering algorithm and complexity. Anyway, I will implement your suggestions/ recommendations in future. Thanks..!

请先登录,再进行评论。

类别

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