【问题标题】:matlab multiple foldersmatlab 多个文件夹
【发布时间】:2012-08-16 12:19:06
【问题描述】:

我有一个包含 50 个文件夹的目录,每个文件夹有 50 个文件。我有一个脚本可以读取每个文件夹中的所有文件并保存结果,但我每次都需要输入文件夹名称。我可以使用任何循环或批处理工具吗?非常感谢任何建议或代码。

【问题讨论】:

    标签: matlab


    【解决方案1】:

    可能有更简洁的方法,但dir 命令的输出可以分配给变量。这为您提供了一个结构,相关字段为nameisdir。例如,假设顶级目录(包含 50 个文件的目录)中只有文件夹,下面将为您提供第一个文件夹的名称:

    folderList = dir();
    folderList(3).name
    

    (请注意,folderList 结构中的前两个条目将用于“.”(当前目录)和“..”(父目录),所以如果你想要第一个包含文件的目录,你必须转到第三个条目)。如果您希望逐个浏览文件夹,可以执行以下操作:

    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” ",这是一个文件。

    【讨论】:

    • 感谢您的快速回复,使用cd(folderList(i))时出现“Argument must contain a string”的错误,
    • 你需要使用cd(folderList(i).name)
    • 维克多说得对——我在修正括号时不小心删除了那部分。我会在答案中解决这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多