|
|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
假如要读取某个文件夹中所有的图片文件:clear all;, \7 U7 G5 }5 g; l- r2 a+ ~8 ]
clc;4 P" ^% u/ v+ S {5 d/ f9 i
%%. l. G0 @+ O1 ^' L/ [ z
slashtype = filesep;- P. b1 ?: P; R9 n: {
% FILESEP Directory separator for this platform. O& V1 {( Q9 N, G
% F = FILESEP returns the file separator character for this platform.3 }1 z6 J* E3 _3 L/ h9 B' p
% The file separator is the character that separates
{% a. n8 z& N% directory names in filenames.
) X' u: `8 W# _9 i$ p/ p% [
6 ?( d& V; ~$ v6 f9 noptions.dir = ['.',slashtype]; % The directory you would like to read
8 |4 P! {% X! I9 J& Z- ^" }
0 L4 d6 I3 r' Y5 D/ n6 ximext = {'.jpg','.bmp','.png','.tiff','.tif'}; % All the file extentions
" E8 p1 s5 T# ]; O: S! G9 c, p. x9 A" W- E
% Read all the files in this directory with the extensions
9 V! Z5 i0 m% j+ x( e- H% u[fileinfo,filenumber] = Read_Dir_Files(options.dir,imext);# b7 V5 O' P8 ?9 {
8 [ V' G% E4 O, k! N9 k) k* n ?% s- o( K3 }$ d
Read_Dir_Files 函数如下:6 S3 Y6 |/ Q7 K0 T" ?4 {. E
( x3 ^* \( Q7 @, B0 h/ a5 R$ \8 H
function [fileinfo,filenumber] = Read_Dir_Files(inputdir,extention)' d2 C* g3 I9 u$ c# m# c: r
% This function read the files with all the input extensions in the directory 'inputdir' 4 [3 _/ A f6 |# `/ S
% Input: inputdir --- the directory you would like to go through: ?7 Z# [1 G/ Q; \8 c
% extention --- cells, have the extension you specified
B2 K; y: Q) I. ~; v. f! b* r% Output: fileinfo --- cells, fileinfo{i}.fullname fileinfo{i}.name
$ B2 ^+ g$ y0 |2 a% fileinfo{i}.extension% ^/ V5 s3 y7 C' W' q# g% \
% filenumber -- the total number of files
$ x' P0 n9 [/ a K" z%
& |8 t, Z9 ?$ Z1 i @& N% Example usage: [fileinfo,filenumber] = Read_Dir_Files('./',{'.m'})& h4 h. D- N: }6 [7 [, J$ A
%. O4 ?: y7 Y" V' L
/ ~ Q9 D( k$ Q' Q
files = dir(inputdir); % get all the name of files and dirs under the folder 'inputdir'; A; c: c2 G1 K) A8 j7 F I/ }% n
% Here files can be file or a directory [2 K/ a" C2 \5 s
% S9 J& _2 l$ W9 O! C0 E6 X
' E c" m: Z3 u4 V% k. a0 X%Loop over all filenames
5 _8 k9 c8 y% T5 z: B1 hii=0;# G! ]9 U& J4 _3 J! M. u8 [
for iF = 1:length(files). A" D) Q& p- _# m8 w9 @
%
/ x2 ~+ m8 `8 s& Q2 a if (~files(iF).isdir)
0 p8 O9 L/ k3 t$ l" Q %
0 D9 K! J! c% }1 w % If name is not a directory get detailed information* A4 X0 P, K, ?2 h
%
1 [6 @+ b8 c# ^' P0 T# Z- N [pathstr, name, ext] = fileparts(files(iF).name);# {" j# p* z e6 B
% [PATHSTR,NAME,EXT,VERSN] = FILEPARTS(FILE) returns the path,1 a; \$ W9 I% J- z! Y
% filename, extension and version for the specified file." W" `9 k, x( A1 t9 N: W7 }( l
% FILEPARTS is platform dependent./ E$ T2 X: F2 T. i6 u
7 G; {% l" O: V4 K! Q %Loop for all the extension% Z. ]$ `6 r, `
for ie=1:length(extention)
5 c! Y u( J4 Q/ N; D %
) X& w: H+ m7 a# ^6 F0 t % Compare to known extentions and add to list
; a% R% t* e$ C6 |/ P" ` %3 h% x+ l; |0 @- a0 }
if (strcmpi(extention{ie},ext))2 ^+ K) F5 P# |/ R1 r
ii = ii + 1;
( g7 B- m& e: M' C8 Z1 j. C5 u ext = ext(2:end);# g ]* O4 b g& e3 [# ?
fileinfo{ii}.fullname = [inputdir,name,'.',ext];0 f& K2 U8 X& g" o- c+ U
fileinfo{ii}.name = name;! W- l. r! }' D9 e
fileinfo{ii}.extension = ext;
' Y# W" [; Q+ A4 y end
! ~. U! U! L/ c& @3 {, N %
7 @; n9 p$ M) H# B$ t end; e/ w) c$ b+ d) N& p! T% l# ` N
%3 Q8 g2 o# b$ _: N
end4 _0 S4 V3 ~, u, ^5 A* ^
%
* B: S( \% M# a! N) Pend: T( J% Z2 j5 \& P" I/ Z4 d Q
filenumber = ii;
1 K) M* R. O* p8 x1 [! Uif filenumber==0( e9 R+ \( C f
fileinfo={};3 ?% d2 j6 s9 s5 ?+ P% F# M
end3 y# U6 [; {" y7 M/ s3 \
! J) E( D$ _& ~7 Q
end
; T, R p6 y6 X4 v7 \ \
( s- j% f# O$ P) i% F8 K+ ?. _/ S8 F2 d5 m3 |! t1 E
+ A! n8 G, |8 ]/ t
|
|