找回密码
 注册
关于网站域名变更的通知
查看: 291|回复: 1
打印 上一主题 下一主题

Matlab读取文件夹中所有特定类型文件

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2020-8-3 14:06 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

EDA365欢迎您登录!

您需要 登录 才可以下载或查看,没有帐号?注册

x
假如要读取某个文件夹中所有的图片文件:clear all;8 K& `1 ?- c8 C% b2 C1 X$ x1 Q
clc;
7 c5 ^4 Y3 m  Q* G/ S3 y; E" k%%
! I& l. u, g7 |6 B, o8 rslashtype = filesep;" e1 }+ ^/ e  e; y6 r" |
%  FILESEP Directory separator for this platform.
+ Q+ S6 ]6 W0 i7 r/ a+ i/ W%     F = FILESEP returns the file separator character for this platform.8 m- d( g5 Z* [/ u- F
%     The file separator is the character that separates
9 C& k: e% v5 L+ b# ]- S- v9 o%     directory names in filenames.1 @0 e1 [. F' `9 r$ o) U3 G

. x6 S+ W: o& E% i# n9 V; \: K1 ioptions.dir = ['.',slashtype]; % The directory you would like to read$ u! o# p! n8 I
/ _3 S. ?6 P2 f0 y' F- U- t& \
imext = {'.jpg','.bmp','.png','.tiff','.tif'}; % All the file extentions7 R- a2 i4 \  k  x( v  C7 e
3 q% u1 Q( q8 c' ]7 F2 R4 \; R
% Read all the files in this directory with the extensions9 O% H5 C/ ]" A6 D- |1 e
[fileinfo,filenumber] = Read_Dir_Files(options.dir,imext);
9 t: x% h) _; b# o: `! y1 j, r6 w8 ]" U, K

5 S6 L( v5 J3 {Read_Dir_Files 函数如下:
+ t# h" j/ H+ @. w- V7 F
# {% r2 D0 E4 I/ ofunction [fileinfo,filenumber] = Read_Dir_Files(inputdir,extention)
* \( U9 \3 ^1 q" R" d2 M7 u/ B% This function read the files with all the input extensions in the directory 'inputdir'
/ z5 C" y  {1 e2 Q/ S( {% Input: inputdir  --- the directory you would like to go through
, ?+ H/ j0 ]6 m0 G2 T; G- d% i) C+ s%        extention --- cells, have the extension you specified
7 n8 G0 t; I: R: P- B% Output: fileinfo --- cells, fileinfo{i}.fullname fileinfo{i}.name% i7 H. V% H/ k& C1 I1 B1 E
%                      fileinfo{i}.extension& `2 `0 Y2 W& L9 u" f
%         filenumber -- the total number of files& n1 ^; y; _6 V! T0 H
%
3 x: o+ r3 l6 O( y* k9 ~- ~% Example usage: [fileinfo,filenumber] = Read_Dir_Files('./',{'.m'})
: ?, p4 F; z- O  K1 i! l5 x%
! T3 _6 \8 K8 d) J- q8 [4 b7 y6 n* g& C/ _' l9 D
files = dir(inputdir);  % get all the name of files and dirs under the folder 'inputdir'
' E3 a' }# Z/ l7 N# U* O% Here files can be file or a directory7 |9 D' ?+ \4 z3 r: V

0 V: _  M2 z1 j1 u' ]" M. x% d* X+ k' v4 W  Q& q
%Loop over all filenames- T* C9 V) s' P, u6 P, Z, M" l3 O
ii=0;
3 m  \8 h8 N9 R% y5 Cfor iF = 1:length(files)- }+ w3 l0 Y- n% K4 H
    %' U5 m* N  e" K" ~
    if (~files(iF).isdir)2 o7 T. B5 f5 P' O
        %, g! [* O9 j* @+ a0 ]2 Z4 @! E  ^
        % If name is not a directory get detailed information7 a* q. ?! w2 T) m# T
        %
# J0 A) M+ f' k; V$ H% @        [pathstr, name, ext] = fileparts(files(iF).name);
8 ?7 w1 u" K% u, U        %     [PATHSTR,NAME,EXT,VERSN] = FILEPARTS(FILE) returns the path,& W& C, t8 |1 ?) f) I9 [3 b: y
        %     filename, extension and version for the specified file.
. v1 j$ b* c, A4 ~9 u% j- h& u        %     FILEPARTS is platform dependent.
7 J# S5 `  `; Z0 D3 p        : @  E7 ~3 h# y* D1 T  a5 u
        %Loop for all the extension
+ f+ k) y1 `: ?- k5 @0 i        for ie=1:length(extention)" K" J, l  U% ]' |; Z. G
            %8 p+ Z7 p5 r! t# C$ _( s# _! U9 @9 o
            % Compare to known extentions and add to list  R/ F5 V* M7 l
            %
% S! D% W* i0 I: i/ `: Q+ N3 i            if (strcmpi(extention{ie},ext))5 K0 ^, u5 S1 S3 M
                ii = ii + 1;& d' h. p# L6 m. K
                ext = ext(2:end);
) H3 V' I) X% n. D) Z1 z                fileinfo{ii}.fullname   = [inputdir,name,'.',ext];
, j4 E2 f/ @" Q( F' h, x7 n                fileinfo{ii}.name       = name;
, n9 K0 }9 Q1 j1 n7 \/ _                fileinfo{ii}.extension  = ext;  R( |. \4 e5 R4 a5 k( f' ]
            end
6 F* x/ B1 {0 L8 W: _! {            %
, Q5 _& d' x* N        end
3 A& k/ X  q! f- H% `" o        %
; T) o4 ^( l* }" N" F- P9 I  F    end4 f; n1 N! w. A' B
    %  ~0 N% e7 [' U* w
end( [6 D2 m' ]. e
filenumber = ii;
7 H6 l2 z: q2 l# Dif filenumber==08 \: e( K$ R% i) O6 i0 M# U+ p
    fileinfo={};9 M) @2 P$ i( Z5 s
end
5 ~. R' B0 h2 O) f& b! J5 F& |' b2 ~4 l6 @8 U$ Y# c8 F6 m
end
/ P  ]9 S3 }( ~
0 B( m: M/ m. V. e1 I9 q  k) J5 k: L4 j( m" j" Z( D

, }: z) e# c- D
  • TA的每日心情

    2019-11-29 15:37
  • 签到天数: 1 天

    [LV.1]初来乍到

    2#
    发表于 2020-8-3 15:13 | 只看该作者
    Matlab读取文件夹中所有特定类型文件
    您需要登录后才可以回帖 登录 | 注册

    本版积分规则

    关闭

    推荐内容上一条 /1 下一条

    EDA365公众号

    关于我们|手机版|EDA365电子论坛网 ( 粤ICP备18020198号-1 )

    GMT+8, 2025-11-25 00:11 , Processed in 0.171875 second(s), 23 queries , Gzip On.

    深圳市墨知创新科技有限公司

    地址:深圳市南山区科技生态园2栋A座805 电话:19926409050

    快速回复 返回顶部 返回列表