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

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

[复制链接]

该用户从未签到

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

EDA365欢迎您登录!

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

x
假如要读取某个文件夹中所有的图片文件:clear all;
: C% Y8 O0 m. Eclc;
5 `' a  j& C% `+ I' d. D%%  K2 a& h. l6 x9 L$ ^; W3 r
slashtype = filesep;  a  M7 r+ k- s" I! [$ U
%  FILESEP Directory separator for this platform.; R+ x9 G% H. b1 n' E8 R5 L" m
%     F = FILESEP returns the file separator character for this platform.
4 z$ W, M- P0 U0 J8 C5 j# {1 m%     The file separator is the character that separates
5 ?7 e+ E9 ?* D4 r, d%     directory names in filenames.# m7 f1 V. f' {! Q+ X0 X

* u, f1 U& z- Q. H9 p+ ]9 `3 X0 Qoptions.dir = ['.',slashtype]; % The directory you would like to read" V9 B. c8 H6 x/ k' X2 O

# x4 d! i% G2 P. U5 yimext = {'.jpg','.bmp','.png','.tiff','.tif'}; % All the file extentions  l% e( {. P$ N" X
9 s5 v1 }9 h6 Q, M
% Read all the files in this directory with the extensions8 Z. O4 _- T* w
[fileinfo,filenumber] = Read_Dir_Files(options.dir,imext);+ @: U! q' ^# X! A# N0 Z  t3 C
( m) y2 }+ Z( k$ f
, x- P3 R. K$ ~/ W9 I* M5 V9 E
Read_Dir_Files 函数如下:" `* ]2 h# g6 z" ~; J5 j! H, m
3 O3 w/ H( |* f& p2 _
function [fileinfo,filenumber] = Read_Dir_Files(inputdir,extention)0 E% C3 K# l' W5 ~7 |6 W! T( E2 h2 }
% This function read the files with all the input extensions in the directory 'inputdir'
  q1 I* @4 }' \' N& r0 g2 L% Input: inputdir  --- the directory you would like to go through
+ ]$ K6 {8 \/ N. Z# u%        extention --- cells, have the extension you specified
# T& j0 n3 H1 e9 S3 x% Output: fileinfo --- cells, fileinfo{i}.fullname fileinfo{i}.name' n+ A- s/ R2 X  M/ L
%                      fileinfo{i}.extension0 g+ A5 M. ^+ @  o( ^: e9 G
%         filenumber -- the total number of files
( u5 v9 B0 t8 K* q%. s; T, R8 J5 x" ^/ e
% Example usage: [fileinfo,filenumber] = Read_Dir_Files('./',{'.m'})9 W: [* [; z& J$ e0 Y$ \' O. ~
%
# x$ m/ Z& v$ F" a$ u/ l
4 s6 j5 Y# G4 _6 Z- z& Nfiles = dir(inputdir);  % get all the name of files and dirs under the folder 'inputdir'' h8 d8 U" {. L
% Here files can be file or a directory
: n" h9 W: V' t2 C2 t1 `  u. K5 F' g. r+ X

& a! l' ~# y3 U%Loop over all filenames3 B, T  z  u( F
ii=0;
6 \, n! O. A, @" W2 j, @for iF = 1:length(files)
7 Y, A- N' x& h3 u7 b8 X$ ^    %' _, h) B7 Z$ u, f7 ?5 v; G
    if (~files(iF).isdir)
- k# T8 F. L' u  P        %# Q! a7 m( o1 I6 O
        % If name is not a directory get detailed information  X! I  a/ P' s
        %
/ }. P- @' g, W1 T& v        [pathstr, name, ext] = fileparts(files(iF).name);- R2 m2 M7 g$ {( X) s
        %     [PATHSTR,NAME,EXT,VERSN] = FILEPARTS(FILE) returns the path,
, B- a* b6 ^4 L. b/ _! ^        %     filename, extension and version for the specified file./ s$ P  v) b8 s! E/ ?
        %     FILEPARTS is platform dependent.
, U* \1 P, \. c7 S        & V! P/ o( s5 `5 b- ?" j
        %Loop for all the extension5 R8 N5 m+ r: Q& x- H+ s7 @
        for ie=1:length(extention)
, t: v. Y' V$ U! z7 \9 N            %
; D! y. f. l* c* Y* n            % Compare to known extentions and add to list3 N* x2 d! ~# U7 z9 ]$ e! p
            %" a9 I! i  Q& q  ]6 ^' U* d
            if (strcmpi(extention{ie},ext)), s/ |3 I  K  d) D) b- P
                ii = ii + 1;1 B. R$ }* B6 s2 S$ e+ c
                ext = ext(2:end);
+ Z2 O0 M. y0 t; v2 Q                fileinfo{ii}.fullname   = [inputdir,name,'.',ext];7 d8 b" Y+ D  c$ Z3 M4 ~( d
                fileinfo{ii}.name       = name;
( h0 ], ^8 g- W% }" T$ [                fileinfo{ii}.extension  = ext;
% f/ l* x( T5 f  ^% `            end
! [& p, p9 j7 u            %
; }2 {! g# ?* H        end
( N6 G4 j5 k5 b$ P1 ]' ~$ A        %
5 L; b- c* G( z, {; I    end
! r3 D+ S! P" H! u2 }& ?    %; ~/ H3 b* h3 E1 ?8 |. t; O
end
+ I# ?3 L. [* h; H. j8 ?. X4 I/ Ufilenumber = ii;' Q6 P& [6 v8 ]% s# G* X- e! R
if filenumber==0' Y" @+ a0 @1 Y- M$ z* o
    fileinfo={};7 C' M& F* T! b$ W
end
) A# P( K4 l4 x0 t, i& p9 R5 J/ h6 ]! L
end
+ b1 E2 s+ {1 ^1 k5 W( q4 J  @! Q- f; V) W/ j" G* P
, A3 s  a, m5 O: a+ V4 J+ f8 D! Q

+ _; B; v8 D  s
  • 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 20:08 , Processed in 0.171875 second(s), 23 queries , Gzip On.

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

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

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