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

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

[复制链接]

该用户从未签到

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

EDA365欢迎您登录!

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

x
假如要读取某个文件夹中所有的图片文件:clear all;
0 Y* u0 n! M" m) oclc;
' {* w! u; j+ D%%
& J+ R3 q. m: c; Tslashtype = filesep;3 M. r" Q  w/ `. H: N' Y) p$ f$ ^
%  FILESEP Directory separator for this platform.
  x; h) `' c" j# o%     F = FILESEP returns the file separator character for this platform.  |! Q; U4 ^! g" y8 K' t0 ?5 e
%     The file separator is the character that separates0 e+ E2 ~, }2 E3 p
%     directory names in filenames.- v/ s1 J: y: l4 O" L0 d

  W1 e" h! {  o7 xoptions.dir = ['.',slashtype]; % The directory you would like to read
" ^) v& S( H, F' d: B- S
& ]& g  |" D" }# s: I5 Dimext = {'.jpg','.bmp','.png','.tiff','.tif'}; % All the file extentions
7 _) W% D1 J5 |. I
6 P; c' \+ b  X% Read all the files in this directory with the extensions
- {; N( N# Y8 R5 H[fileinfo,filenumber] = Read_Dir_Files(options.dir,imext);
4 v3 N  K0 B" }: M
8 w3 a; V. V: T) t3 s; Q6 v7 w8 E! C$ Q% x/ J9 q
Read_Dir_Files 函数如下:2 j0 B4 q5 E! D! e. G, U9 S

& F% w, a; j' t* I; V& Vfunction [fileinfo,filenumber] = Read_Dir_Files(inputdir,extention)
6 |7 J5 q8 y& q) }, L' }  S% This function read the files with all the input extensions in the directory 'inputdir'
2 ?, s5 b3 p4 X. _% Input: inputdir  --- the directory you would like to go through3 y1 p. Z3 M+ I- r: ^6 s
%        extention --- cells, have the extension you specified( F+ @( f. R5 _/ N4 U. e$ F
% Output: fileinfo --- cells, fileinfo{i}.fullname fileinfo{i}.name
- ?+ F3 x, r  R  ~9 }, e  |%                      fileinfo{i}.extension
: y6 h# t" o- B" C8 d% y  J" c%         filenumber -- the total number of files  H# Z& L( n2 V( d4 K5 t4 b
%+ u7 R1 X" B( J4 k* \: P& D
% Example usage: [fileinfo,filenumber] = Read_Dir_Files('./',{'.m'})
4 t9 e! o2 i, Q8 d%
% W2 K, C  F7 L0 ?% Q( I5 r4 W( _; X& w: Z# W
files = dir(inputdir);  % get all the name of files and dirs under the folder 'inputdir'
$ H: M6 Y4 M  n( w% @! m% a0 w% Here files can be file or a directory
. C* @  H4 w  m# M/ P. A. O) I8 e  l& o, k1 ?/ {

$ H! Y) {# L# Y%Loop over all filenames) q' M; U- Y, t. Z9 y
ii=0;9 r0 [8 u& E1 e
for iF = 1:length(files), _# C7 H. {- s3 O2 Q$ ^3 c
    %
3 q) J# L( s0 b  H+ W0 Z    if (~files(iF).isdir)6 a3 b7 M  T- d8 N
        %9 [! M5 N3 H) @8 C4 `% P8 G
        % If name is not a directory get detailed information+ z1 k+ g9 k8 B. Z
        %& |: @0 }- Y/ i0 k$ x: c
        [pathstr, name, ext] = fileparts(files(iF).name);
" {' T' F. M& N5 E1 u2 ?        %     [PATHSTR,NAME,EXT,VERSN] = FILEPARTS(FILE) returns the path,  L  A! V* x( J6 W) J4 ]" }
        %     filename, extension and version for the specified file.1 C! i2 Y" V9 O) D
        %     FILEPARTS is platform dependent.+ R  F1 r( R6 a" v  u5 U7 R5 \
        $ C0 ~  A  k- g* h
        %Loop for all the extension
( c4 t) c! I, x9 X        for ie=1:length(extention)
2 x9 ?! K5 {0 ^/ R3 ]* i2 M            %% X3 p& w9 {: s% w
            % Compare to known extentions and add to list
2 `/ ~5 B9 k' |+ X            %7 U. Z2 `! p( T" x1 U6 M
            if (strcmpi(extention{ie},ext))
$ r3 l4 S  C9 E& i5 a- M                ii = ii + 1;0 X# L- j, J. v$ L) p% E
                ext = ext(2:end);2 s8 X+ \, B' i6 P
                fileinfo{ii}.fullname   = [inputdir,name,'.',ext];
7 u, _& O  I1 S6 [                fileinfo{ii}.name       = name;$ J1 ]) K5 V/ W' j
                fileinfo{ii}.extension  = ext;
9 j  @, ?) P, C* v( Z! y& ?            end
7 t4 W" o" a5 `8 X9 A6 H2 O* C            %% h5 t/ V2 G+ U4 G
        end
' S) V/ H* A- o' r        %
% O" b  b9 ^( }    end1 c2 ]. N8 M4 L% B& {& D/ d
    %
8 F- j3 W. ?9 O, }end
! U% b4 C" `3 c8 `% [& C8 \filenumber = ii;5 d' `1 m8 n1 R3 _$ e
if filenumber==0' k- Z- I: t' P* o( M
    fileinfo={};
% b6 H/ A) B' ?2 v; S) J2 Gend
! P5 i) ^: X1 i2 K& e6 H
% F8 a% T! k/ G6 r) s2 {end
7 F0 U6 Z& I* x+ _6 b4 T
$ @8 G# i+ u% v; E) k. U. [/ R- a, E
- c" |( e5 u. Y
  • 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-24 19:35 , Processed in 0.140625 second(s), 24 queries , Gzip On.

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

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

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