【问题标题】:loop through folders matlab遍历文件夹matlab
【发布时间】:2015-08-13 17:12:53
【问题描述】:

我需要遍历一个文件夹(称为数据),其中包含许多不同名称的文件夹。我需要挑选一些特定的文件夹,每个文件夹都以相同的单词(Variance)开头。然后,当在这些“Variance ...”文件夹中时,我需要打开两个文件并通过一个函数来获取一些信息。每个文件夹(F_1 和 F_0)的文件名称相同:

1) 遍历文件夹,直到找到具有特定名称开头的子文件夹。

2) 进入子文件夹

3) 打开其中的两个文件并将它们用于一个功能(我可以做到这一点)

4) 回到原来的文件夹(Data)继续循环寻找特定的子文件夹,重复直到找到所有的子文件夹。

【问题讨论】:

  • 您可以将dir 与通配符一起使用(请参阅文档),但缺点是它不是递归的。您可以编写自己的递归dir 函数(不是太难),但我也可以推荐subdir from the MATLAB File Exchange,它使用与dir 相同的输入和输出语法。

标签: matlab loops directory


【解决方案1】:

像这样? (为我自己的目的测试和运行,所以我知道它在注释行中描述的范围内有效)

function status =  rview(thedir)

yesdirs = rdir(thedir,'isdir');
% when numel(yesdirs) ==0 there are no directories; note that yesdirs does
% not include . or .. by design.
if numel(yesdirs),
    %assume no idiot every mixed files and directories in a given dir
    for j =1:numel(yesdirs),
        disp(sprintf('going to %s',yesdirs(j).name));
        eval(sprintf('cd ''%s'' ', yesdirs(j).name));
        thedir = sprintf('d%d',j);
        stat(j).(thedir) = rview('.');
        cd('..');
    end
else
    %no subdirectories.. worked our way to the bottom
    your_function_here() 
    %  indicator of where we were

    stat= ['did_' cd];
end

status = stat;
end

【讨论】:

  • 抱歉,我现在已经改了。现在我从一个文件夹“mainFolder”开始,其中包含我需要的所有文件夹,只需要循环浏览它们,分别打开它们,然后取出我需要的文件。 mainFolder = dir(fullfile('文件夹名'));对于 k = 1:numel(mainFolder) subFolder = mainFolder(k);这样做的想法是,它将遍历文件夹并打开每个单独的子文件夹,然后进入我的函数并在函数结束时返回主文件夹
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 2021-07-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-03
相关资源
最近更新 更多