如何将我的结果存储在递归函数中

时间:2015-11-19 13:39:51

标签: matlab recursion

我有一个 matlab函数,用于列出文件夹中的所有文件,包括其子文件夹。我可以全部显示它们,但我不知道如何将它们存储在我的输出变量中。

function out = searchfolder(varargin)
if nargin<1
    helpdlg('no input floder');
    return;
else
curfolder=varargin{1};
end
if ~isdir(curfolder) || length(dir(curfolder)) < 3
return;
end

children = dir(curfolder);
%eliminate '.' and '..' 
children = children(3:end);
allfiles = {};
for i=1:length(children)
   child = children(i);
   thisone = fullfile(curfolder,child.name);
    if child.isdir
       searchfolder(thisone);
    else
       disp(thisone);

    end
end

那么如何找出这个问题?

1 个答案:

答案 0 :(得分:0)

有了excaza的帮助,我有代码:

function out = searchfolder(varargin)
out = {};
if nargin<1
%        helpdlg('no input floder');
    return;
else
curfolder=varargin{1};
end
if ~isdir(curfolder) || length(dir(curfolder)) < 3
return;
end 
children = dir(curfolder);
%eliminate '.' and '..' 
children = children(3:end);
allfiles = {};
for i=1:length(children)
   child = children(i);
   thisone = fullfile(curfolder,child.name);
    if child.isdir
       out = [ out ;searchfolder(thisone)];
    else
       disp(thisone);
    out = [out;{thisone}];
    end
end