Matlab的多个文件夹 [英] matlab multiple folders
问题描述
我有一个包含50个文件夹的目录,每个文件夹有50个文件.我有一个脚本来读取每个文件夹中的所有文件并保存结果,但是我每次都需要输入文件夹名称.我可以使用任何循环或批处理工具吗?任何建议或代码都将不胜感激.
I have one dir with 50 folders, and each folder has 50 files. I have a script to read all files in each folder and save the results, but I need to type the folder name every time. Is there any loop or batch tools I can use? Any suggestions or code greatly appreciated.
推荐答案
也许有更简洁的方法,但是dir
命令的输出可以分配给变量.这为您提供了一个结构,相关字段为name
和isdir
.例如,假设顶层目录(包含50个文件的目录)中仅包含文件夹,则以下内容将为您提供第一个文件夹的名称:
There may be a cleaner way to do it, but the output of the dir
command can be assigned to a variable. This gives you a struct, with the pertinent fields being name
and isdir
. For instance, assuming that the top-level directory (the one with 50 files) only has folders in it, the following will give you the first folder's name:
folderList = dir();
folderList(3).name
(请注意,folderList结构的前两个条目将用于."(当前目录)和".."(父目录),因此,如果要第一个包含文件的目录,则必须转到第三个条目).如果您希望一一浏览文件夹,则可以执行以下操作:
(Note that the first two entries in the folderList struct will be for "." (the current directory) and ".." (the parent directory), so if you want the first directory with files in it you have to go to the third entry). If you wish to go through the folders one by one, you can do something like the following:
folderList = dir();
for i = 3:length(folderList)
curr_directory = pwd;
cd(folderList(i).name); % changes directory to the next working directory
% operate with files as if you were in that directory
cd(curr_directory); % return to the top-level directory
end
如果顶级目录包含文件和文件夹,则需要检查folderList结构中每个条目的isdir
-如果为"1",则为目录,如果为"0" ,这是一个文件.
If the top-level directory contains files as well as folders, then you need to check the isdir
of each entry in the folderList struct--if it is "1", it's a directory, if it is "0", it's a file.
这篇关于Matlab的多个文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!