2013-04-09 77 views
0

我有一个有3个文件夹的目录。这3个文件夹中的每个文件夹都有其他数量的文件夹。并且在这些文件夹的每个文件夹中都有一个我想要在这些文件上运行代码的文件列表。例如: 它是这样的: MainFolder有SubFolder1,SubFolder2。 SubFolder1具有SubSubFolder1,SubSubFolder2,SubSubFolder3。 SubFolder2具有SubSubFolder1,SubSubFolder2。 每个SunSubFolders都有一些文件。 我想要一个脚本,我给它的MainFolder路径,它通过每个子文件夹和子文件夹,并对此子文件夹中的文件进行操作,并通过此子文件夹的名称保存工作区。因此,在上面的示例中,在对SubSubFolder1中的文件执行一些操作之后,结果工作区将保存在名称为SubSubFolder1.mat的位置。在Matlab中递归列出目录

请问我是否有人可以帮助我,因为这对我来说是相当紧迫的。 非常感谢您的亲切协助和考虑。

更新:

我已经做到了,但另一个问题起来了当我访问这些文件在SubSubFolders,并设法使操作它说:“文件‘[00000000] .PGM’不能因为:没有这样的文件或目录“。 如何解决这个问题?

这是我的代码:

D = rdir('Hussein/*/*');   %// List of all sub-directories 
for k = 1:length(D) 
    currpath = D(k).name;     %// Name of current directory 
    [pathstr, name, ext] = fileparts(currpath); 
    %// Operate on all .txt files in currpath 
    l = dir(fullfile(currpath, '*.pgm')); %// Listing of all relevant files 
    filenames={l.name}'; 

    nfiles=length(filenames) 
    %images=zeros(240, 320, 1000); 
    idx=1; 

    strtidx=1; 
    endidx=nfiles; 
    step=1; 

    waitbar(0); 
    for i=strtidx:step:endidx 
     fn=fullfile('', filenames{i}); 
     tmp=padarray(ut_pgmread(fn), 1, 'post'); 
     %figure(1); imagesc(tmp); colormap(jet(4096)); colorbar; 

     images(:, :, idx)=tmp; idx=idx+1; 

     waitbar(i/nfiles); 
    end 

    close(findall(0,'Tag','TMWWaitbar')); 
    name='/Volumes/Untitled/work/'+name; 
    save (name, '-v7.3'); 

    %for m = 1:length(F) 
    % currfile = F(m).name;    %// Name of current file 

     %// Do something with currfile... 
    %end 

    %// Write output (if any) in currpath... 
end; 
+0

只是所以搜索,你就会有几乎完整的答案: http://stackoverflow.com/questions/8748976/list子文件夹中的文件夹-matlab-only-subfolders-not-files – 2013-04-09 14:29:33

+0

@Robocop这不是一个递归目录列表,所以它不是重复的。 – 2013-04-09 14:46:19

+0

@EitanT我已经更新了这个问题,所以如果你可以请检查一下。 – Tak 2013-04-09 16:23:01

回答

3

看来你正在寻找的dir递归版本,所以你可能会发现从MATLAB文件,您的目的交换有用的enhanced rdir tool。使用增强rdir,你的代码看起来沿着这些路线:

%// List all sub-directories under MainFolder recursively 
D = rdir('MainFolder\**\*.');    %// List of all sub-directories 
for k = 1:length(D) 
    currpath = D(k).name;     %// Name of current directory 

    %// Operate on all .txt files in currpath 
    F = dir(fullfile(currpath, '*.txt')); %// Listing of all relevant files 
    for m = 1:length(F) 
     currfile = F(m).name;    %// Name of current file 

     %// Do something with currfile... 
    end 

    %// Write output (if any) in currpath... 
end;