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

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

[复制链接]

该用户从未签到

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

EDA365欢迎您登录!

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

x
假如要读取某个文件夹中所有的图片文件:clear all;! f, G1 |, D% |8 _
clc;. U' a" T5 g4 k& y
%%, o; p4 p) |  T1 Y' B; M' j
slashtype = filesep;3 ^- h3 r' I( |/ B6 n
%  FILESEP Directory separator for this platform.  ^1 \7 W/ X1 `. V7 S8 H  _6 D; Y
%     F = FILESEP returns the file separator character for this platform.8 e* a# H, G' o2 d2 Z" F
%     The file separator is the character that separates; }: `, j$ Y9 ?( @
%     directory names in filenames.  M) @. a+ b9 T* @, a" M% f% a
4 C+ T! I, J) W0 Z/ A% E% w7 q
options.dir = ['.',slashtype]; % The directory you would like to read! b* b0 t( t( m* E* e7 w! e: i

$ N' T* _* ]9 V' Uimext = {'.jpg','.bmp','.png','.tiff','.tif'}; % All the file extentions- r0 F8 W7 G* t
8 ]1 e# [  U5 G0 i6 h6 a, b
% Read all the files in this directory with the extensions( y$ a+ y0 ?4 z- t5 q/ x/ ~
[fileinfo,filenumber] = Read_Dir_Files(options.dir,imext);2 N, h, L' A, {1 f6 p

4 m5 E% O! c0 f) E3 n" y8 C2 o/ v) @  s! O
Read_Dir_Files 函数如下:9 o- b5 j6 f' l& a
7 z  \7 J( J: i  h7 g; k4 i9 @
function [fileinfo,filenumber] = Read_Dir_Files(inputdir,extention)
4 `' |4 c, R, m* P% This function read the files with all the input extensions in the directory 'inputdir' ) @) `9 G) r  {9 U2 e) s& n5 K: |
% Input: inputdir  --- the directory you would like to go through
; \, |3 Z6 D: f  ^8 Q7 Z  [' v%        extention --- cells, have the extension you specified
9 X- P: [7 Q* [8 J8 A4 ^% Output: fileinfo --- cells, fileinfo{i}.fullname fileinfo{i}.name9 Q" y& C9 d6 N8 g8 g9 X6 y
%                      fileinfo{i}.extension' v5 D8 e" |( z. S; T( K; c
%         filenumber -- the total number of files8 R4 Z, N6 R! l
%" I# y, S: x. p$ |" ^
% Example usage: [fileinfo,filenumber] = Read_Dir_Files('./',{'.m'}): P( Y0 o8 [: B+ k- z4 u4 e2 s
%$ g3 M/ W- o3 }( ?! h4 f5 r
7 z9 t+ j! E0 S. \4 t1 w
files = dir(inputdir);  % get all the name of files and dirs under the folder 'inputdir'
* g0 L, I, j# x( ^+ S- k9 q6 g3 a% Here files can be file or a directory1 a  d& N4 F2 f( _/ j7 a" ]: F

4 N6 e# k5 `5 E6 U  T
8 }. C# t; C2 i%Loop over all filenames
3 R( F! w* \- J; c  X# A0 b: N" c' v7 Wii=0;
' m3 }/ \% `: K7 [' M5 x' g1 \for iF = 1:length(files)
, p: ~2 K1 O2 w9 g) v9 r    %
8 C" ~8 g5 Y; L, v7 F- U; r/ E    if (~files(iF).isdir)
6 c# C% J. H7 z" y) F, D        %3 y1 l, h- ~' D$ ^5 p
        % If name is not a directory get detailed information! ~' N' w. v' R3 c4 ]: n5 ~
        %
1 b0 \$ N6 i0 P  G& m& v/ F        [pathstr, name, ext] = fileparts(files(iF).name);0 D6 V" t' {+ r/ J' b8 A
        %     [PATHSTR,NAME,EXT,VERSN] = FILEPARTS(FILE) returns the path,: h" D) v% V+ f
        %     filename, extension and version for the specified file.
" F) ]+ f3 V; s( \% ]8 {' L6 s  d        %     FILEPARTS is platform dependent.
4 V. H8 h+ V* h5 l        
# p. P; N1 B; e/ g: G  U9 k        %Loop for all the extension5 W' z$ Y. w4 |- m9 K9 \5 x; |) N9 Q
        for ie=1:length(extention)
! }  ~* g/ g8 `( Z, }            %
2 k$ L$ ?4 S& y0 F! Z            % Compare to known extentions and add to list6 x  H% w$ ^; D+ S  p5 q
            %
$ _" X. w: J4 F8 W            if (strcmpi(extention{ie},ext))
. u7 [( U* ^0 n8 o! K; j  N                ii = ii + 1;
8 Y! {/ _' t8 }1 b. h4 j& ^) o5 D                ext = ext(2:end);: N2 a  r3 c9 P+ H1 Q/ t, p& f
                fileinfo{ii}.fullname   = [inputdir,name,'.',ext];
' q1 a% B& d+ X3 h5 ~' G                fileinfo{ii}.name       = name;
2 w% ?5 i8 ?1 b3 {( X1 s2 b8 x/ X8 u                fileinfo{ii}.extension  = ext;; [8 ~% E% {& q" k* {6 W/ }8 [
            end. H: p0 T$ D, F2 Y! C
            %+ K6 I, r4 |& o# t
        end7 g. F: R" P; A9 ?# ~0 `4 }
        %
6 ~! U$ ]$ c4 b5 d    end$ L5 w) S; p. t0 Y$ ]
    %: b$ L' m8 Y" O' c4 d& ^
end+ @$ e$ @4 O9 a% c. T
filenumber = ii;) r) c' C7 e* g
if filenumber==06 T; K9 @7 G( \8 {8 h+ ^- d
    fileinfo={};
0 r' V. w" g1 Z$ d/ @0 z5 eend
' i+ z- t- M8 [% o& L9 V6 Q6 {6 c. a+ n; ~6 }6 U
end
" l! x  N% S! G# f# ~' w: i
/ t1 g1 G. k$ z# M$ _9 k9 Z/ m; ~* u7 e5 m( U
6 ^% U* z7 Y8 C
  • 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-8-17 20:51 , Processed in 0.109375 second(s), 23 queries , Gzip On.

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

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

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