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

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

[复制链接]

该用户从未签到

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

EDA365欢迎您登录!

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

x
假如要读取某个文件夹中所有的图片文件:clear all;
5 N$ P+ d6 n9 Zclc;
  r/ D& W1 I: K+ x/ A1 \2 d%%
# T" p6 U# I2 \+ m+ islashtype = filesep;- V+ m$ R! ]+ d
%  FILESEP Directory separator for this platform.
1 O9 j5 K! X0 V  P0 m%     F = FILESEP returns the file separator character for this platform.- t+ p9 L3 n, l, z! P7 I0 ^
%     The file separator is the character that separates! U8 h( H" J+ ^" H* ^
%     directory names in filenames.0 K& A; H& a0 g! m
  ?  P/ _1 H2 W% K
options.dir = ['.',slashtype]; % The directory you would like to read& G" _" V( ~( Q- @6 N6 M' j; n5 y

$ _" _0 A/ u& @7 \2 fimext = {'.jpg','.bmp','.png','.tiff','.tif'}; % All the file extentions
/ f; p: e& F6 m8 a. D: T+ l- @' }5 T, L0 h
% Read all the files in this directory with the extensions) s3 J3 j- h2 |6 x3 N
[fileinfo,filenumber] = Read_Dir_Files(options.dir,imext);8 T! O! g8 T# x0 y) o

* h! n. N& D1 g" K6 D$ P  h& F' M3 R9 q& a, E
Read_Dir_Files 函数如下:
) ~; S' c3 L$ Z9 y7 S- j) r" A$ E: [5 V5 J4 |
function [fileinfo,filenumber] = Read_Dir_Files(inputdir,extention)
( F; a0 u* C% }; C% This function read the files with all the input extensions in the directory 'inputdir'
7 Q; h3 d: S$ M  V- t$ q! B4 [% Input: inputdir  --- the directory you would like to go through
# c6 ?1 ]. k) j  K5 o+ C2 U. i. z%        extention --- cells, have the extension you specified- n+ D% t6 ~+ {& T; C7 w% ]% z
% Output: fileinfo --- cells, fileinfo{i}.fullname fileinfo{i}.name& S' V2 }8 f1 R- L
%                      fileinfo{i}.extension
" ?% R' }1 d  Q% y& b% I%         filenumber -- the total number of files
, [) B2 {+ j) J: y& i( c%' W  o( p0 F1 O- W) W. t+ u
% Example usage: [fileinfo,filenumber] = Read_Dir_Files('./',{'.m'})8 T8 j$ f; h# d7 M1 F0 }2 W& D' ^
%, \4 X8 |+ D* [1 V" L
9 r! ?$ g3 v7 q( `5 M$ E) _, f
files = dir(inputdir);  % get all the name of files and dirs under the folder 'inputdir'
' l5 f9 E" p6 ^  m, I. O% Here files can be file or a directory2 R/ M& `, G% l" }% t$ Z* T

% H' J5 v( X( ]; l! ^! z" W3 j$ T2 P2 F6 _
%Loop over all filenames& _  L: ~7 a# l7 n- J# a7 J; f
ii=0;
5 n3 W6 v# ]$ t8 W/ y- gfor iF = 1:length(files)* t' _# H9 @- [( k2 ]* A5 D
    %
1 Y! I7 d( b$ U( g: l    if (~files(iF).isdir)
: q+ d+ n8 L7 y% J        %
+ O: Q6 Q4 s1 {  ]) U        % If name is not a directory get detailed information
7 O8 S' l/ `1 P: M        %
  w4 b  J! O# s+ @) N, @7 Q        [pathstr, name, ext] = fileparts(files(iF).name);
: B' }3 h2 I% `. M        %     [PATHSTR,NAME,EXT,VERSN] = FILEPARTS(FILE) returns the path,
2 W8 \: v' V) W3 @3 u: Z        %     filename, extension and version for the specified file.9 R! u5 D4 P9 G3 D1 x
        %     FILEPARTS is platform dependent.5 `; y* F: F- |/ W' x
        
( V  i6 u; c2 n/ x" D4 P        %Loop for all the extension$ I. b: y: J2 [, o
        for ie=1:length(extention)* |) j9 P5 @4 x# N2 t8 T
            %$ x3 w6 @3 D6 w! e9 o
            % Compare to known extentions and add to list
0 D! }7 L: k- w            %
2 V! i8 Z2 ?' Q( f# Q/ v! C            if (strcmpi(extention{ie},ext)): c5 W6 P% x! x- _3 ~2 ]; ?; Y
                ii = ii + 1;  U& u$ [0 c; A: W; O' Y
                ext = ext(2:end);
5 P2 Y8 D" [0 ~' z" U. E                fileinfo{ii}.fullname   = [inputdir,name,'.',ext];: z- |) I8 _* E9 C) D* b$ V& w* l
                fileinfo{ii}.name       = name;
5 z( Q  d& n4 [, w7 d: `- z                fileinfo{ii}.extension  = ext;
( ^' R' z; E+ }1 I            end+ n  ?) m; E$ k/ b: n( e0 ?
            %8 A/ l* n( S# f7 T! a" H9 z6 K
        end# g+ f8 u& N6 `$ D3 ^5 _4 p
        %& t/ B1 u) x7 _  ]( r  p
    end
% o5 g7 V9 `, A& b    %* E3 |& d1 v) n) L
end
- V) n5 x7 t( I5 Y2 W' O9 jfilenumber = ii;* X3 I- T$ u' K* ~* Y1 Z, @
if filenumber==0
  Q& ?9 E7 t$ ?/ v3 n    fileinfo={};( I% U! L: v- e+ y6 ?' [4 C+ d
end
( u+ [; B( M: i% I" }: q: V0 Y
/ x' f5 ?# O7 @! iend
: ]- g' Q- E; I3 S- ]
" M6 f0 x5 U! F6 a2 |  G2 E* @% N2 ]$ M; x9 @2 H, K# [. n+ K5 k0 s! g- i

) f2 E. W( x8 m) _1 a+ z$ Z
  • 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-7-27 14:53 , Processed in 0.109375 second(s), 23 queries , Gzip On.

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

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

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