|
|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
假如要读取某个文件夹中所有的图片文件:clear all;$ i5 z# {: P+ t5 y& ?
clc;
. m0 a1 Z# Y& k+ C. F* {" y, m%%
9 l0 t0 O6 ?: Z+ `$ dslashtype = filesep;
( @ M- j) W- f% FILESEP Directory separator for this platform.
) v8 c6 |# }+ D1 ?0 C. W- i% F = FILESEP returns the file separator character for this platform.+ j* p+ f1 D9 P
% The file separator is the character that separates
' g# r; p+ f/ G& h% directory names in filenames.
" [9 U. w. Z0 U
( l' U) X/ y! u+ f; x8 {) p% ?options.dir = ['.',slashtype]; % The directory you would like to read
( o5 ]/ Z2 S3 \" M( @, m
: O/ u! E9 A" {* O) \) ]8 Yimext = {'.jpg','.bmp','.png','.tiff','.tif'}; % All the file extentions! A/ @2 F# H2 _. i, E
* c& v9 {, P0 s# }' ~0 G% Read all the files in this directory with the extensions
# `% @/ H# g- [) F3 g& V" e+ D8 y[fileinfo,filenumber] = Read_Dir_Files(options.dir,imext);
0 m/ h5 g2 x+ Y& M: ?9 O8 x$ t# d# j9 V- E3 l' j5 _
$ \+ Z5 J8 |7 Y) ~2 W5 {
Read_Dir_Files 函数如下:9 q( p# S0 f6 A$ p
8 n7 X! u" e& zfunction [fileinfo,filenumber] = Read_Dir_Files(inputdir,extention)
. o! r4 d! u3 r: ?. ^% This function read the files with all the input extensions in the directory 'inputdir' : V' C( t* Z) e6 h8 Z- O
% Input: inputdir --- the directory you would like to go through% w8 `8 L- l4 z7 }, S f
% extention --- cells, have the extension you specified
8 \2 }0 a, T" b D% Output: fileinfo --- cells, fileinfo{i}.fullname fileinfo{i}.name
+ h' ]/ Y2 F O6 W1 N7 R6 E0 Q% fileinfo{i}.extension6 i: \& @2 n! ^% `1 r* d+ C. L( } o
% filenumber -- the total number of files1 v9 K9 p, [) K) G$ }( Z
%# Z' P6 h3 b% }( S& ^1 A
% Example usage: [fileinfo,filenumber] = Read_Dir_Files('./',{'.m'})* j d: j. z: k# q) @) e! \
%+ r5 n& I/ p+ K9 h$ t
[8 e- v |2 F9 Y
files = dir(inputdir); % get all the name of files and dirs under the folder 'inputdir'
M' n6 @/ O8 a/ Q7 A% Here files can be file or a directory8 b y/ B5 b, b6 K5 {
2 u' f/ s# Q1 v3 v; l/ x a- q
4 s# M" D' z+ `+ c" m5 g
%Loop over all filenames
5 ^, k" S& f, S/ X6 B. uii=0;- J( y: x, n0 M! {8 Z
for iF = 1:length(files)# g1 X! Q% w0 y( G2 b4 Z
%" d( v$ J/ g* q S: O J* w
if (~files(iF).isdir)( L& t- O; I7 d0 F: f; C6 V
%8 s9 ^7 _) U5 Z- ?5 ?6 C W
% If name is not a directory get detailed information
; L7 e* {' B* i/ S %
$ }. G4 H+ a7 }3 Z [pathstr, name, ext] = fileparts(files(iF).name);$ _( U7 G J/ ?3 a. E3 U
% [PATHSTR,NAME,EXT,VERSN] = FILEPARTS(FILE) returns the path,1 k' V/ H" n- O V% h- Y" @0 s
% filename, extension and version for the specified file.0 E0 K7 ^' v7 b j5 d) ?8 m5 t- Z# u
% FILEPARTS is platform dependent.
" W* \8 ~7 _$ z: `4 b
: e+ O' Q, |, N" W3 t! q2 H/ h %Loop for all the extension
; f8 X h3 L c% y% l% t9 | for ie=1:length(extention). y% D2 O% ^6 t# B
%
& [; B* E+ R: o6 g: z& k % Compare to known extentions and add to list0 ], y) t% l9 {4 A* u
%- s- v. {" |/ e& {
if (strcmpi(extention{ie},ext))! q- g: p+ y6 G* G {* y
ii = ii + 1;2 m1 |7 h! W" | Y, I3 x
ext = ext(2:end);1 q0 M/ O9 R' ^$ a- l
fileinfo{ii}.fullname = [inputdir,name,'.',ext];
$ T5 g V" M: s5 x8 y5 D fileinfo{ii}.name = name;# I( f8 O# }% a
fileinfo{ii}.extension = ext;; H9 N) S* _7 p, w2 U, R* x4 f
end8 f' c4 u0 o% l* }+ a" g
%
0 p/ z0 b( C" w3 _ end: `) h, }3 O# W& N5 f! T( I
%( r* b, x/ A9 o6 X8 F
end( N% r* B! Y0 ~# \2 u! U
%
' a) `& v m. L- Vend
& H. O% w- e7 I& V+ Nfilenumber = ii;# [6 P- ?/ N; V# E. }2 Z
if filenumber==0
- ^" _. @4 l/ K! K0 R fileinfo={};
# ?, j% V0 E* ^. p; O7 d, Oend# p! m1 \/ [' g
2 k1 w0 Z; u+ s% C$ ~
end* l1 R" [$ h' c7 j0 I2 o
5 o( M6 U* Z* X/ K4 y) n) a
9 T/ G z; [: S7 X- b ]9 `7 x1 Z& G( _: P/ [7 i
|
|