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

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

[复制链接]

该用户从未签到

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

EDA365欢迎您登录!

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

x
假如要读取某个文件夹中所有的图片文件:clear all;/ C, b: q- K0 @0 k7 U# o0 X8 q
clc;
) t+ q& j4 A- z/ G3 F3 o. E%%0 ]5 V+ ?: K5 |: m
slashtype = filesep;
% P6 d& p# v% \- R%  FILESEP Directory separator for this platform.0 j. Z6 w' a. y& [
%     F = FILESEP returns the file separator character for this platform.5 c1 @6 w: F9 D6 P; J
%     The file separator is the character that separates3 }5 ^% J/ }' n# i
%     directory names in filenames.
9 E6 ^5 f& b; _4 n" U. `4 W5 O$ V- ~0 v. t% K
options.dir = ['.',slashtype]; % The directory you would like to read4 p4 [& R9 ?  [5 S6 c1 E" x! y
, p/ a. J. B! A/ N7 V
imext = {'.jpg','.bmp','.png','.tiff','.tif'}; % All the file extentions# {  W" v& k+ _6 R9 {* e

5 p; U& ?0 {& y, N3 `7 V* V% Read all the files in this directory with the extensions. F7 i* _2 b0 V3 y6 f* `1 c
[fileinfo,filenumber] = Read_Dir_Files(options.dir,imext);
2 j8 ^. y3 P8 }0 ?7 y% P2 d& |/ E% M) p9 x! @

% h+ m, C3 n8 xRead_Dir_Files 函数如下:
  h+ p5 `2 `1 |. f( ~5 Q8 B8 n- @  X3 ]) B. J
function [fileinfo,filenumber] = Read_Dir_Files(inputdir,extention)7 b, W) a" f9 Q) n7 U- d
% This function read the files with all the input extensions in the directory 'inputdir'
& b8 V  X! W& m3 D1 [$ z5 n2 l% Input: inputdir  --- the directory you would like to go through
2 ]% n+ J; t0 U% B%        extention --- cells, have the extension you specified
/ A% k& m+ Q& r( M+ q1 ^% Output: fileinfo --- cells, fileinfo{i}.fullname fileinfo{i}.name, o: l! ]6 r7 a3 d; N; \
%                      fileinfo{i}.extension3 V0 e0 A3 f; M1 [' b
%         filenumber -- the total number of files
- k( K! [# d1 U  G/ A" M( j$ {3 Y%
6 j4 s6 i* y$ ~/ U$ O% Example usage: [fileinfo,filenumber] = Read_Dir_Files('./',{'.m'})
1 U7 [" V, Q# d1 \* S%
) y/ Z( m+ o) e* q+ z
7 l% g3 Y5 H1 R2 {# jfiles = dir(inputdir);  % get all the name of files and dirs under the folder 'inputdir'
/ q8 N& [; J+ a0 _9 j. X% Here files can be file or a directory7 p- J8 i" C: u
- D5 D7 }' I1 }& o7 M5 Y

9 Q5 P6 C$ n, |0 z( e( E%Loop over all filenames
; c( p+ ^1 K3 H# W# Sii=0;
* s8 A3 d) u4 Z+ E! m' i  |5 cfor iF = 1:length(files)
7 }: X% ^4 Y7 `9 A# ]1 ^5 X    %
7 `( }* N/ r# C    if (~files(iF).isdir)
' i8 V( M, x1 A. J4 y        %" }- V; d  u  q7 S- S# f) Q5 _* ?
        % If name is not a directory get detailed information
* h, t1 q4 h" h/ e5 M+ \        %
+ \2 c5 q! u) e7 i' x        [pathstr, name, ext] = fileparts(files(iF).name);, r/ I# ^0 |. `5 l: j  y
        %     [PATHSTR,NAME,EXT,VERSN] = FILEPARTS(FILE) returns the path,* y2 Z6 e, N& m: o' C. ^
        %     filename, extension and version for the specified file.
) D7 _; I5 Y& {3 p) f4 }) b        %     FILEPARTS is platform dependent.0 J- |8 Y5 E# W  l' n; q
        
. R* p5 d$ I: e" k# O' s) V! q* [: I        %Loop for all the extension, O) n5 @" }# y1 b4 Z
        for ie=1:length(extention)( I' I# O, X) D9 C
            %3 r) J# \/ N; c* E
            % Compare to known extentions and add to list1 Y$ X$ P# V, J' A( f0 C
            %
  p6 X4 o1 J: W. ?' r6 N            if (strcmpi(extention{ie},ext))
6 t) H; |. f- [+ L5 W9 Y                ii = ii + 1;
2 [4 U4 ~; B2 J; [0 {/ l5 h) O+ [                ext = ext(2:end);# q9 W; V& r5 ~- z7 u9 }. c# t( I
                fileinfo{ii}.fullname   = [inputdir,name,'.',ext];
. O  r3 \! ~6 h" x' L) s% G( A) Y                fileinfo{ii}.name       = name;
! ^4 u5 C/ l) N- R7 c+ y1 L                fileinfo{ii}.extension  = ext;" g/ v. p, J7 {: a+ ?! R! v1 [
            end  U3 \: c' ^. B' f
            %$ U  |/ q+ ?$ J0 Z
        end
* k0 X2 f% t& q3 u2 V7 J, M        %! g2 V: Z+ m% P$ H$ H
    end
  ^: P7 |0 g  c+ U5 q3 ?9 Q    %, A: l7 A; }, v4 _5 h; ?
end
9 m6 |! E& e3 t5 ^" n0 Afilenumber = ii;
! M% v0 x- i* V5 a$ |4 Sif filenumber==08 h/ E( j2 p' y4 @
    fileinfo={};
& F: K& C( w" `4 z8 Yend
! p. q+ m2 m& {; ?; b
* z! B: ]* y8 i. hend
( V/ E" D' g: @4 Q
: m4 I. D9 M& C2 e$ z  n3 h/ [% g8 y2 X& n# m" K2 L- Z
  B! v  O/ N9 y) V: [
  • 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-10-8 01:42 , Processed in 0.125000 second(s), 23 queries , Gzip On.

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

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

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