2014-10-10 100 views
0

我在matlab中有函数(带有一个实际调用函数的包装),递归地查找计算机上给定HDD中的所有.mat文件。在每次返回时,它都会给出特定文件夹中的文件,因为驱动器上的数百个文件夹(按日期组织)有数百个返回。在Matlab中递归函数的返回列表

我想使这些文件的一个列表(或矩阵),以便另一个脚本可以使用此列表来完成它的工作。

实际返回结构列表(包含文件信息的字段)。 返回值总是一个宽度和一个长度,具体取决于文件夹中有多少个文件。

总之,我想知道如何获取递归函数的所有返回并将它们放入一个列表/矩阵。

任何提示将不胜感激! 谢谢

function direc = findDir(currentDir) 

dirList = dir(currentDir); 
if 2 == length(dirList) 
    direc = currentDir 
    files = dir([currentDir '*.mat']) 


    return 
end 

dirList = dirList(3:length(dirList)); 
fileListA = dir([currentDir '*.mat']); 

if 0==isempty(fileListA) 
    direc = currentDir 
    files = dir([currentDir '*.mat']) 


    return 

end 

for i=1:length(dirList) 
    if dirList(i).isdir == 1 

     [currentDir dirList(i).name '\']; 

     findDir([currentDir dirList(i).name '\']); 

end 

end 


end 
+0

请澄清你的问题是什么,并发布你使用的相关代码。要做到这一点,编辑您的问题 – 2014-10-10 19:48:14

+0

谢谢,我是新手。 – jdrudds 2014-10-10 20:11:10

+1

也许您在寻找[this](http://stackoverflow.com/a/2654459/1586200)。修改特定类型的文件很容易,在你的情况下,'.mat'。 – 2014-10-10 20:17:26

回答

0

您可以使用​​,其搜索递归所有文件和输出以及有关这些文件信息的结构数组。然后删除文件夹,并只保留名称以'.mat'结尾的文件。

要检查文件名是否以'.mat'结尾,请使用regexp。请注意,如果字符串name太短,则直接比较name(end-3:end)=='.mat'将会失败。

currentDir = 'C:\Users\Luis\Desktop'; %// define folder. It will be recursively 
%// searched for .mat files 
[~, f] = fileattrib([currentDir '\*']); %// returns a structure with information about 
%// all files and folders within currentDir, recursively 
fileNames = {f(~[f.directory]).Name}; %// remove folders, and keep only name field 
isMatFile = cellfun(@(s) ~isempty(regexp(s, '\.mat$')), fileNames); %// logical index 
%// for mat files 
matFileNames = fileNames(isMatFile); 

可变matFileNames是字符串的单元阵列,其中每个字符串是一个.mat文件的全名。

+0

这工作得很好。非常感谢你! – jdrudds 2014-10-13 14:37:28