码迷,mamicode.com
首页 > 其他好文 > 详细

Matlab学习:读取指定文件夹及其五级子文件夹内的文件

时间:2016-01-31 03:02:14      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:

OpenCV2.4.X版本提供了三个函数来读取指定目录内的文件,它们分别是:

 

(1)GetListFiles:读取指定目录内所有文件,不包含子目录;

 

(2)GetListFilesR:读取指定目录及其子目录(仅一级子目录)内所有文件;

 

(3)GetListFolders:读取指定目录内所有目录,不包含文件;

 

然而,Matlab中并没有对应的函数,有人可能会说dir不就可以吗,但dir返回的值还进行一些处理我们才能用的,如移除返值中包含的父目录及当前目录。这里我就写了一段代码来读取指定目录及其子目录(递归五级子目录)内所有文件,相当于GetListFilesR,但递归子目录层次更深。具体代码如下:

function files = GetListFilesR(top1dir)

cn = 1;
files = cell(0);

top1 = dir(top1dir);
len1 = length(top1);
for a = 1:len1
    %1.1是本目录或父目录
    if strcmp(top1(a).name, .) || strcmp(top1(a).name, ..)
        continue;
    end
    
    %1.2不是目录
    if top1(a).isdir == 0
        files{cn} = strcat(top1dir, /, top1(a).name);
        cn = cn + 1;
        continue;
    end
    
    %1.3是目录
    top2dir = strcat(top1dir, /, top1(a).name);
    top2 = dir(top2dir);
    len2 = length(top2);
    for b = 1:len2
        %2.1是本目录或父目录
        if strcmp(top2(b).name, .) || strcmp(top2(b).name, ..)
            continue;
        end

        %2.2不是目录
        if top2(b).isdir == 0
            files{cn} = strcat(top2dir, /, top2(b).name);
            cn = cn + 1;
            continue;
        end 
        
        %2.3是目录
        top3dir = strcat(top2dir, /, top2(b).name);
        top3 = dir(top3dir);
        len3 = length(top3);  
        for c = 1:len3
            %3.1是本目录或父目录
            if strcmp(top3(c).name, .) || strcmp(top3(c).name, ..)
                continue;
            end

            %3.2不是目录
            if top3(c).isdir == 0
                files{cn} = strcat(top3dir, /, top3(c).name);
                cn = cn + 1;
                continue;
            end 
            
            %3.3是目录
            top4dir = strcat(top3dir, /, top3(c).name);
            top4 = dir(top4dir);
            len4 = length(top4);  
            for d = 1:len4
                 %4.1是本目录或父目录
                if strcmp(top4(d).name, .) || strcmp(top4(d).name, ..)
                    continue;
                end

                %4.2不是目录
                if top4(d).isdir == 0
                    files{cn} = strcat(top4dir, /, top4(d).name);
                    cn = cn + 1;
                    continue;
                end  
                
                %4.3是目录
                top5dir = strcat(top4dir, /, top4(d).name);
                top5 = dir(top5dir);
                len5 = length(top5);   
                for e = 1:len5
                     %5.1是本目录或父目录
                    if strcmp(top5(e).name, .) || strcmp(top5(e).name, ..)
                        continue;
                    end

                    %5.2不是目录
                    if top5(e).isdir == 0
                        files{cn} = strcat(top5dir, /, top5(e).name);
                        cn = cn + 1;
                    end                
                end%5级 
            end%4级  
        end%3级    
    end%2级
end%1级
end

 

Matlab学习:读取指定文件夹及其五级子文件夹内的文件

标签:

原文地址:http://www.cnblogs.com/dzyBK/p/5172347.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!