先給出函數
function [ files ] = scanDir( root_dir ) files={}; if root_dir(end)~='/' root_dir=[root_dir,'/']; end fileList=dir(root_dir); %擴展名 n=length(fileList); cntpic=0; for i=1:n if strcmp(fileList(i).name,'.')==1||strcmp(fileList(i).name,'..')==1 continue; else fileList(i).name if ~fileList(i).isdir % 如果不是目錄則跳過 full_name=[root_dir,fileList(i).name]; cntpic=cntpic+1; files(cntpic)={full_name}; % end else files=[files,scanDir([root_dir,fileList(i).name])]; end end end end
測試文件夾如下所示:
文件夾1如下所示:(文件夾1里面的新建文件夾有3個圖片,文件夾1共10個文件)
文件夾2如下所示:(文件夾2共2個文件)
測試:
files=scanDir('C:\Users\yangsu\Desktop\test2');
得到一個1×12cell的數據