|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
假如要读取某个文件夹中所有的图片文件:clear all;
- ]6 W% c5 M: o% Z0 \7 ~clc;
3 N( D% B q0 q! R2 y%%
8 v2 p$ F, _/ D! hslashtype = filesep;
" e) l R& j/ R0 h2 ]7 r# Z% FILESEP Directory separator for this platform.0 J" M1 _$ b- k/ @- z5 I
% F = FILESEP returns the file separator character for this platform.
, n# Q6 P: m* {' z& W5 D6 h: X4 ~% The file separator is the character that separates/ O! d5 C0 b& F: P9 t' d* K+ y
% directory names in filenames.
5 Z3 j% l% y% s: t' _. Z
7 w1 k2 v0 I- d% z" m$ `options.dir = ['.',slashtype]; % The directory you would like to read6 s5 N" H- Z0 `
' V" T4 R) z- ]" A1 ^5 ^
imext = {'.jpg','.bmp','.png','.tiff','.tif'}; % All the file extentions8 q& u( s6 P: }
! W5 x$ b# S& V6 N7 [, s% Read all the files in this directory with the extensions- S) R/ i* X; Y: ^
[fileinfo,filenumber] = Read_Dir_Files(options.dir,imext);
' P# g3 u1 }6 ?' t' t; L, ]9 W. c8 P- W; o' X- E& w
- @% i9 o! K( U# Z1 H7 n
Read_Dir_Files 函数如下:$ C: x3 t- K, H- g$ _# L! G7 O
3 f8 J: ^0 T. p+ zfunction [fileinfo,filenumber] = Read_Dir_Files(inputdir,extention)
% v: }( E/ ^$ X t% This function read the files with all the input extensions in the directory 'inputdir' ; S# L* j' T! v) s0 G/ j
% Input: inputdir --- the directory you would like to go through- A' P, l" k) O1 S5 N5 |8 N
% extention --- cells, have the extension you specified( C2 x/ p0 x W) }
% Output: fileinfo --- cells, fileinfo{i}.fullname fileinfo{i}.name% ^* A# z! E# w
% fileinfo{i}.extension0 C7 k {# n% s/ g1 i
% filenumber -- the total number of files+ U F# n, @2 L
%
& O+ r" |) l1 N% Example usage: [fileinfo,filenumber] = Read_Dir_Files('./',{'.m'})1 S* G' Y1 o* H( y9 |4 F
%
' U% Z9 J4 X1 B. X( T
k+ z7 Y% y/ _, o4 J* xfiles = dir(inputdir); % get all the name of files and dirs under the folder 'inputdir'
l9 T/ L+ O1 m" n% Here files can be file or a directory* n, C: W4 O& g) H0 l0 U6 y
# }: E* K: J" h. n
7 p8 x1 J5 D7 g%Loop over all filenames3 P9 j# q0 S! ]+ Q0 K/ M4 B, A3 M
ii=0;4 V6 s, L8 D6 R
for iF = 1:length(files)
; ?4 t# |4 b' W$ W5 x %
. Q0 A; U/ V" A3 G: ^7 X0 B if (~files(iF).isdir)
9 @, u- y1 K7 `. s %
* W# v+ c! }- Q/ Z" ` % If name is not a directory get detailed information8 q* H: Z% t x- `
% T+ U) t" s5 |; m1 F5 m
[pathstr, name, ext] = fileparts(files(iF).name);
G; L U! F8 L- W- ? % [PATHSTR,NAME,EXT,VERSN] = FILEPARTS(FILE) returns the path,
[- c3 O* s3 v2 U % filename, extension and version for the specified file., e) i+ L9 }7 T& }' I4 `
% FILEPARTS is platform dependent.
1 Q3 ~& j; F) E! d6 Y L' b 5 g2 M C6 Z7 K/ |( q" ?
%Loop for all the extension6 e5 l% Y: C0 q
for ie=1:length(extention)
- j' m4 ]$ \4 j n5 |. J* l6 [' ?- B8 p' _ %
' `& j0 G# B p$ Z % Compare to known extentions and add to list" L1 |! L5 }6 n3 S( s/ O- ^
%% w) ~ ~9 ?3 x4 I
if (strcmpi(extention{ie},ext))
% M: ]+ J. n& W" y ii = ii + 1;5 T0 f% ^7 h! \2 e
ext = ext(2:end);
6 K# G- M4 f1 d! U6 p" X fileinfo{ii}.fullname = [inputdir,name,'.',ext];6 Z" P( o4 {9 T( Y# u9 c
fileinfo{ii}.name = name;" F; s0 C9 T5 \8 I2 F
fileinfo{ii}.extension = ext;
$ M. Y( n7 a0 O- z: W- p3 z end
8 Y3 C7 {9 r0 W6 W %* h. S, N% C$ t3 z& I+ F
end2 G) B' C; O. z
%; X/ q8 `3 z d( D
end
- D! j& l. ?$ U6 X& D4 d7 K %4 C; z. D9 n: w
end
% @. Z3 d; s2 N7 o" sfilenumber = ii;6 M' b0 h: Q5 j2 t$ D) r
if filenumber==06 ?" e. l3 C# Z/ Q
fileinfo={};
9 | p! y% p2 s" p, m! Zend
8 E% s' B( ~) e# J' C5 `( }: _$ X# i+ U& T1 N* F
end U# ^1 e6 z$ f q
# b2 W5 k3 l7 m* O X3 q0 e1 ?! M
. h2 D1 J( X6 x6 {" ^ s- u6 C( Z- f |
|