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

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

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

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

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

function files = GetListFilesR(top1dir, ext)

%1.指定输出参数类型
files = cell(0);

%2.获取所有文件
cn = 1;
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级

if strcmp(ext, 'none') == 1
    return;
end

%3.获取指定后缀名文件
tmp = files;
len = length(tmp);
files = cell(0);
k = 1;
for i = 1:len
    %获取文件名
    filelen = length(tmp{i});
    indexs = strfind(tmp{i}, '.');
    lastIndex = indexs(end);
    fileext = tmp{i}(lastIndex+1:filelen);

    %判断是否为指定类型文件
    if strcmp(fileext, ext) == 1
        files{k} = tmp{i};
        k = k + 1;
    end
end

end

  

Matlab学习:读取指定文件夹及其五级子文件夹内的文件的更多相关文章

  1. 文件夹中含有子文件夹,修改子文件夹中的图像存储格式(python实现)

    文件夹中含有子文件夹,修改子文件夹中的图像存储格式,把png图像改为jpg图像,python代码如下: import os import cv2 filePath = 'C:\\Users\\admi ...

  2. JAVA中删除文件夹下及其子文件夹下的某类文件

    ##定时删除拜访图片 ##cron表达式 秒 分 时 天 月 ? ##每月1日整点执行 CRON1=0 0 0 1 * ? scheduled.enable1=false ##图片路径 filePat ...

  3. Android 读取手机某个文件夹目录及子文件夹中所有的txt文件

    1. activity_main.xml文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/andro ...

  4. Python扫描指定文件夹下(包含子文件夹)的文件

    扫描指定文件夹下的文件.或者匹配指定后缀和前缀的函数. 假设要扫描指定文件夹下的文件,包含子文件夹,调用scan_files("/export/home/test/") 假设要扫描 ...

  5. Android中读取assets文件夹中的子文件夹内容

    文件结构如下:assets/info/info AssetManager am = this.getResources().getAssets(); InputStream input = null; ...

  6. NSIS如何对一整个目录文件夹(包括子文件夹和其中的文件)压缩

    原来不加/r参数,NSIS编译器就会不认识文件夹啊. File /r [dir] Reference: http://stackoverflow.com/questions/7973242/nsis- ...

  7. asp.net 遍历文件夹下全部子文件夹并绑定到gridview上

    遍历文件夹下所有子文件夹,并且遍历配置文件某一节点中所有key,value并且绑定到GridView上 Helper app_Helper = new Helper(); DataSet ds = n ...

  8. MATLAB检查指定路径中的子文件夹中的文件名中是否带有空格

    测试文件夹为: clear;close all;clc; %% %程序实现的功能 %检查指定路径中的子文件夹中的文件名中是否带有空格,并去掉文件名中的空格 %% %程序中用到的之前不清楚的函数如下 % ...

  9. c++读取文件夹及子文件夹数据

    这里有两种情况:读取文件夹下所有嵌套的子文件夹里的所有文件  和 读取文件夹下的指定子文件夹(或所有子文件夹里指定的文件名) <ps,里面和file文件有关的结构体类型和方法在 <io.h ...

随机推荐

  1. android dimens 读取 px&dp问题

    1.dimens.xml文件:     <resources> <dimen name="area_margin_top">100dp</dimen& ...

  2. Character 比较注意先要转换成字符串类型

    Character tmp = '/'; if("/".equals(tmp)){ System.out.print("error"); } if(" ...

  3. 移动设备上的媒体查询 CSS media queries for mobile device

    CSS的媒体查询虽然在传统的互联网页面可能发挥的余地不是很大,但是自从苹果和安卓的风靡之后,移动平台上的web开发变得越来越流行了,同时CSS的媒体查询可谓派上了大用场了. 以下为翻译内容,原文来自这 ...

  4. trunc函数

    date 为必要参数,是输入的一个日期值 fmt 参数可忽略,是日期格式,用以指定的元素格式来截去输入的日期值.忽略它则由最近的日期截去 下面是该函数的使用情况: trunc(sysdate,'yyy ...

  5. 用Opera Mobile调试手机版网页【转】

    注意:新版本的opera已经采用webkit内核,没有dragonfly了. 要下载12版的http://get.geo.opera.com/pub/opera/win/1216/int/Opera_ ...

  6. @GeneratedValue - fancychendong的专栏 - 博客频道 - CSDN.NET

    登录|注册 收藏成功 确定 收藏失败,请重新收藏 确定 标题 标题不能为空 网址 标签 摘要 公开 取消收藏 分享资讯 传PPT/文档 提问题 写博客 传资源 创建项目 创建代码片 设置昵称编辑自我介 ...

  7. Newtonsoft.Json学习笔记

    Newtonsoft.Json,一款.NET中开源的Json序列化和反序列化类库(下载地址http://json.codeplex.com/). 下面是Json序列化和反序列化的简单封装: /// & ...

  8. PowerShell连接中国Azure

    由于China Azure与Global Azure属于不同的服务,适用于Global Azure的命令并不完全适用于China Azure.目前,Add-AzureAccount命令地址指向Glob ...

  9. 使用 Windows PowerShell 来管理和开发 windowsazure.cn 账户的特别注意事项

    6月6日,微软面向中国大陆用户开放了Microsoft Azure公众预览版的申请界面.大家可以申请免费的 beta 试用,收到内附邀请码的通知邮件后只需输入激活码即可开始免费试用.具体网址为: ht ...

  10. C#实现监控网络流量

    本文转载自:http://blog.csdn.net/zhanjianshinian/article/details/8177851 public partial class NetJiankongF ...