非常感谢matlabsky的smachine
我看了genpath()的源码,类似编了我的函数,达到了目的
我的源码:
find_path.m
function file_path=find_path(start_path)
%------------------------------------------------------
%参考内建函数genpath
%本函数返回所给文件夹中所有文件完整路径
%------------------------------------------------------
file_path=[];
file=dir(start_path);
isfile=logical(~cat(1,file.isdir));
for i=3:length(isfile)
if isfile(i)
file_path=[file_path fullfile(start_path,file(i).name) pathsep];
else
file_path=[file_path find_path(fullfile(start_path,file(i).name))];
end
end
end
这样返回的是把找到的所有文件完整路径(包含文件名)连成了一个字符串,字符串之间按平台路径分隔符分割(windows下是;)
后续需要可以用regexp(find_path('your_path'),';','split')分割出来每个路径