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

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

[复制链接]

该用户从未签到

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

EDA365欢迎您登录!

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

x
假如要读取某个文件夹中所有的图片文件:clear all;$ i5 z# {: P+ t5 y& ?
clc;
. m0 a1 Z# Y& k+ C. F* {" y, m%%
9 l0 t0 O6 ?: Z+ `$ dslashtype = filesep;
( @  M- j) W- f%  FILESEP Directory separator for this platform.
) v8 c6 |# }+ D1 ?0 C. W- i%     F = FILESEP returns the file separator character for this platform.+ j* p+ f1 D9 P
%     The file separator is the character that separates
' g# r; p+ f/ G& h%     directory names in filenames.
" [9 U. w. Z0 U
( l' U) X/ y! u+ f; x8 {) p% ?options.dir = ['.',slashtype]; % The directory you would like to read
( o5 ]/ Z2 S3 \" M( @, m
: O/ u! E9 A" {* O) \) ]8 Yimext = {'.jpg','.bmp','.png','.tiff','.tif'}; % All the file extentions! A/ @2 F# H2 _. i, E

* c& v9 {, P0 s# }' ~0 G% Read all the files in this directory with the extensions
# `% @/ H# g- [) F3 g& V" e+ D8 y[fileinfo,filenumber] = Read_Dir_Files(options.dir,imext);
0 m/ h5 g2 x+ Y& M: ?9 O8 x$ t# d# j9 V- E3 l' j5 _
$ \+ Z5 J8 |7 Y) ~2 W5 {
Read_Dir_Files 函数如下:9 q( p# S0 f6 A$ p

8 n7 X! u" e& zfunction [fileinfo,filenumber] = Read_Dir_Files(inputdir,extention)
. o! r4 d! u3 r: ?. ^% This function read the files with all the input extensions in the directory 'inputdir' : V' C( t* Z) e6 h8 Z- O
% Input: inputdir  --- the directory you would like to go through% w8 `8 L- l4 z7 }, S  f
%        extention --- cells, have the extension you specified
8 \2 }0 a, T" b  D% Output: fileinfo --- cells, fileinfo{i}.fullname fileinfo{i}.name
+ h' ]/ Y2 F  O6 W1 N7 R6 E0 Q%                      fileinfo{i}.extension6 i: \& @2 n! ^% `1 r* d+ C. L( }  o
%         filenumber -- the total number of files1 v9 K9 p, [) K) G$ }( Z
%# Z' P6 h3 b% }( S& ^1 A
% Example usage: [fileinfo,filenumber] = Read_Dir_Files('./',{'.m'})* j  d: j. z: k# q) @) e! \
%+ r5 n& I/ p+ K9 h$ t
  [8 e- v  |2 F9 Y
files = dir(inputdir);  % get all the name of files and dirs under the folder 'inputdir'
  M' n6 @/ O8 a/ Q7 A% Here files can be file or a directory8 b  y/ B5 b, b6 K5 {
2 u' f/ s# Q1 v3 v; l/ x  a- q
4 s# M" D' z+ `+ c" m5 g
%Loop over all filenames
5 ^, k" S& f, S/ X6 B. uii=0;- J( y: x, n0 M! {8 Z
for iF = 1:length(files)# g1 X! Q% w0 y( G2 b4 Z
    %" d( v$ J/ g* q  S: O  J* w
    if (~files(iF).isdir)( L& t- O; I7 d0 F: f; C6 V
        %8 s9 ^7 _) U5 Z- ?5 ?6 C  W
        % If name is not a directory get detailed information
; L7 e* {' B* i/ S        %
$ }. G4 H+ a7 }3 Z        [pathstr, name, ext] = fileparts(files(iF).name);$ _( U7 G  J/ ?3 a. E3 U
        %     [PATHSTR,NAME,EXT,VERSN] = FILEPARTS(FILE) returns the path,1 k' V/ H" n- O  V% h- Y" @0 s
        %     filename, extension and version for the specified file.0 E0 K7 ^' v7 b  j5 d) ?8 m5 t- Z# u
        %     FILEPARTS is platform dependent.
" W* \8 ~7 _$ z: `4 b        
: e+ O' Q, |, N" W3 t! q2 H/ h        %Loop for all the extension
; f8 X  h3 L  c% y% l% t9 |        for ie=1:length(extention). y% D2 O% ^6 t# B
            %
& [; B* E+ R: o6 g: z& k            % Compare to known extentions and add to list0 ], y) t% l9 {4 A* u
            %- s- v. {" |/ e& {
            if (strcmpi(extention{ie},ext))! q- g: p+ y6 G* G  {* y
                ii = ii + 1;2 m1 |7 h! W" |  Y, I3 x
                ext = ext(2:end);1 q0 M/ O9 R' ^$ a- l
                fileinfo{ii}.fullname   = [inputdir,name,'.',ext];
$ T5 g  V" M: s5 x8 y5 D                fileinfo{ii}.name       = name;# I( f8 O# }% a
                fileinfo{ii}.extension  = ext;; H9 N) S* _7 p, w2 U, R* x4 f
            end8 f' c4 u0 o% l* }+ a" g
            %
0 p/ z0 b( C" w3 _        end: `) h, }3 O# W& N5 f! T( I
        %( r* b, x/ A9 o6 X8 F
    end( N% r* B! Y0 ~# \2 u! U
    %
' a) `& v  m. L- Vend
& H. O% w- e7 I& V+ Nfilenumber = ii;# [6 P- ?/ N; V# E. }2 Z
if filenumber==0
- ^" _. @4 l/ K! K0 R    fileinfo={};
# ?, j% V0 E* ^. p; O7 d, Oend# p! m1 \/ [' g
2 k1 w0 Z; u+ s% C$ ~
end* l1 R" [$ h' c7 j0 I2 o
5 o( M6 U* Z* X/ K4 y) n) a

9 T/ G  z; [: S7 X- b  ]9 `7 x1 Z& G( _: P/ [7 i
  • 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 22:14 , Processed in 0.140625 second(s), 24 queries , Gzip On.

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

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

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