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

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

[复制链接]

该用户从未签到

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

EDA365欢迎您登录!

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

x
假如要读取某个文件夹中所有的图片文件:clear all;* m8 f( Q8 K( p+ @+ d% a' `1 a
clc;! I4 A& ~2 n4 u4 }" h4 ]
%%& [0 n$ s/ a% H7 N; k4 C% f
slashtype = filesep;" R  L& H/ R% p' h" o7 ]
%  FILESEP Directory separator for this platform.8 t) W! U( R6 d; N  z
%     F = FILESEP returns the file separator character for this platform." z! ~( |3 E% S5 l3 _3 ~+ ~$ Z$ T* E
%     The file separator is the character that separates# [3 e5 y7 o, B7 s' j
%     directory names in filenames.
3 x8 f9 g4 x$ l; V. r
# K/ \# k8 w, J; _options.dir = ['.',slashtype]; % The directory you would like to read5 L; ~, h0 y6 J- {

( H2 d7 m7 G2 e: h: S+ P, q, @imext = {'.jpg','.bmp','.png','.tiff','.tif'}; % All the file extentions/ y, b% y9 Y# x

8 a# v, k: ]$ {; _& k: k  U% Read all the files in this directory with the extensions
! f* h4 f6 K# z0 W[fileinfo,filenumber] = Read_Dir_Files(options.dir,imext);
& O: M/ m) j, {% g: w: o9 B7 b1 V5 O$ j! f! S5 z$ \

) a. T* ~7 B& }! Q, CRead_Dir_Files 函数如下:, `% W% z  {; Z

0 S5 i* m; o) T7 z7 B9 R' u) yfunction [fileinfo,filenumber] = Read_Dir_Files(inputdir,extention)0 U8 L( t6 u9 [& ]
% This function read the files with all the input extensions in the directory 'inputdir' , ?: ?  ], ?& i; `: u
% Input: inputdir  --- the directory you would like to go through
: x- C& U. }5 S%        extention --- cells, have the extension you specified# N9 i; `- J  n3 q
% Output: fileinfo --- cells, fileinfo{i}.fullname fileinfo{i}.name
  `# r- S8 |. h8 }# p%                      fileinfo{i}.extension5 S5 y! F% ]5 E5 x3 V! c( x
%         filenumber -- the total number of files9 c6 X8 g( w, ?9 q8 g. K0 _. E3 u
%2 h7 _, S7 ^" v* j3 z- W% H
% Example usage: [fileinfo,filenumber] = Read_Dir_Files('./',{'.m'})* D. q/ v1 ~/ S, P- A
%
% \9 ^  C# X8 Z3 A# D& I! p& c1 d% J1 o: H: Y" U3 Y8 m3 P
files = dir(inputdir);  % get all the name of files and dirs under the folder 'inputdir'# P$ R7 ]0 `/ }1 H
% Here files can be file or a directory7 E4 B- x8 ~) [' E8 K: q  y: P0 D
3 k. T9 P; C8 J- g' F& H

& N2 w" o4 x" E8 F%Loop over all filenames0 x# T, n6 \( K
ii=0;
1 l9 }" Q* U! E; tfor iF = 1:length(files)
4 K! h) y) H/ ?( T. @2 Q    %
4 k, F8 F" B+ R$ M* X: }: R    if (~files(iF).isdir)6 y- ?; j2 I# b6 h7 S5 W
        %/ b! \9 g+ d1 n! C, x% I8 k/ R
        % If name is not a directory get detailed information; r& m% `. R  S  R! v0 o3 r% k& e
        %5 ~  `4 K# B6 Q) l" r2 ]
        [pathstr, name, ext] = fileparts(files(iF).name);
' a8 A8 ~. p8 B: l: r+ |; ~( f* S9 ?        %     [PATHSTR,NAME,EXT,VERSN] = FILEPARTS(FILE) returns the path,
7 P7 Z& C; C8 K' D! u        %     filename, extension and version for the specified file.1 j/ d  g, w! M, P7 g) P
        %     FILEPARTS is platform dependent.
" W% }8 m8 r; m" X' q6 }0 D2 X) @7 D        
( l; w- n+ ], k        %Loop for all the extension! p% R1 X0 _. [2 ?
        for ie=1:length(extention)
* `) v" }& [  D4 I/ n            %$ Z7 j4 Q0 c9 V2 |2 E6 h, H
            % Compare to known extentions and add to list0 y+ s  |5 Q6 D4 Y0 v
            %
) A/ X, E* X1 [0 x( }# m$ N9 y: H9 J            if (strcmpi(extention{ie},ext))
; n  r+ g8 Y: e9 g0 @                ii = ii + 1;* ^2 M' j# g0 w. h0 ?3 f
                ext = ext(2:end);# f: J$ R* i' f- Z6 f! {
                fileinfo{ii}.fullname   = [inputdir,name,'.',ext];6 s- k8 h; u1 h, b4 P$ }2 \" t! I' B
                fileinfo{ii}.name       = name;
) R: k/ T. J7 c+ j) u' O- _" ]( L                fileinfo{ii}.extension  = ext;
$ @. ?7 `  R) g. Z0 [( ?! T8 _            end; j' M4 ~3 B6 K$ y/ N
            %5 T9 V0 |+ i# Z+ @
        end7 w: `2 J% X( q/ _1 Z& N5 D9 H: N
        %
2 y" U  A0 L6 a2 y    end, y% h. @* h- n9 v: ~& z8 g% x& {
    %
) r  @5 B' N5 j  m: J! @6 \: |+ Uend( f, W) l1 n4 W4 g; Q
filenumber = ii;
; |$ n% K7 U+ K& g; f. K4 zif filenumber==0
3 z9 W. {1 q* s" ^    fileinfo={};2 b+ W' J6 u# @, c! g1 V# e
end3 v, A0 J) R5 C
# z! g+ m1 W' F
end& A6 m" r( w1 q- |$ `* H  _

6 I, y2 ^; d& [  Q) g+ M  f+ d6 H* p+ k% c+ @  N6 Z7 ^. W& s, }

2 T! d+ C1 v* _! f, _6 m
  • 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-4 23:57 , Processed in 0.125000 second(s), 23 queries , Gzip On.

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

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

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