EDA365电子论坛网

标题: matlab批量读入数据文件的方法 [打印本页]

作者: uperrua    时间: 2020-3-17 09:42
标题: matlab批量读入数据文件的方法
网上搜集的几个代码,很有用,保存。
4 n/ i5 a0 s! H$ I8 J. J1 J3 k4 u% |- G4 L/ }2 |* {
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
& U. N0 i: v6 [1 b; A0 v
' _0 [. z" l9 n- }3 K+ P# P1)要读入的文件下的文件名称依序列的方式命名,如a1b.mat, a2b.mat,...,ajb.mat,...
6 B+ S$ p2 M% `% U
$ _! t1 n) g5 o4 F9 O  L( R1 H% q. a   循环读入1 x/ ~) b- I9 Z# h$ I

5 C, A% A: m4 ]2 n5 M   filepath='';%文件夹的路径
; `  m9 z; c) }$ a" d" X6 {0 z" t# R2 X0 t9 l
   for i=1:n  %n是要读入的文件的个数& g8 T# M2 k0 Q4 ]% k: h# ^. c

3 i2 b/ @5 U8 K9 w4 I4 e# b       load([filepath 'a' num2str(i) 'b' '.mat'])
5 A3 ?) n& W, K" C  ?8 S& F+ c) x/ C, ]( H. g& E
   end, X* U0 b! H  d2 b5 x8 @5 b
4 r) }* ~* ]1 k' W% ]

) E. {" w' L! Y+ A
" q6 C  g4 q4 P, |, {: D2)文件夹下的文件名称无规律
! f% |  H6 i0 v
# B& `9 r+ p4 @8 h" G    如,文件夹里是n幅图像(.jpg) 和一些数据(其他类型),现在要读出所有的图像
- M: V5 V' X) Y$ e
) I( i3 F- q8 k( I$ Q8 ]     a,先得到文件路径. F9 P) ~: i& u4 R
        di = dir('文件路径*.jpg');) ]0 N7 s4 b" C
     b,读入" F( v. ]! O0 Z; \, M
       for k= 1:length(di)# _2 b1 t, z. p+ k! v. X: s+ L: b
           I(k,:,: ) = imread(['文件路径',di(k).name]);
; n6 O# A9 P4 Y+ Y4 V       end  a: ]. P0 p$ I) t" e+ L8 J. f
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
, [* C( e* v8 |% I2 [; x. l主程序:
  P5 Q/ U0 S' C3 U3 }* o& E) R/ l: n& x# o8 @& m) q3 a

9 u5 j: Y2 I; F  P3 Z( Z5 u- Y& j) p
clc;
. o1 I( |0 {5 ]3 x/ R- eclear;# S9 m( P: @0 x- X
fidin=fopen('title.txt','r');$ Z3 ]# {3 C1 w+ \6 X+ A
/ h4 s& }6 l0 y& d
fidout=fopen('result.txt','w');
* n! ~8 _6 N, p! N0 d* ~5 D" @& L& K9 Y
while ~feof(fidin)                  %while ~feof 表示 若 未读到文件末尾 则 继续 循环
1 |" w% h1 q- c) s2 {    wellname=fgetl(fidin);          %如果没有使用fgetl函数,则feof判断没有作用,这将是一个死循环!4 k) k$ m* R# o. j
    titleline=strcat(wellname,'.txt');4 a2 l% i; U: w* H2 c  h- A$ p
    [efc_dpt,efc_poro,efc_perm,efc_ratio]=numprocessor(titleline);7 L3 T  b7 D0 t) M$ a
    efc_rst=[efc_dpt,efc_poro,efc_perm,efc_ratio];5 }! O! m  Q* j
    fprintf(fidout,wellname);
- S2 y, C  @& W2 s: ^9 ^    %fprintf(fidout,'%s %s %s %sn',efc_rst);
/ b6 ]5 Z! j) k% O$ C    fprintf(fidout,'%8.1f %6.2f %6.2f %6.4fn',efc_rst);
9 d8 M4 Q3 H% e$ X* d6 Yend4 W( h4 j. x# `  Z1 J  y
fclose(fidin);& O. D5 b$ H' L8 @
fclose(fidout);2 i$ s" j/ j& p
5 w2 `; ~% l+ W  s) o+ i( _) G6 v

9 ~8 p$ l0 n, s: [1 O& c2 E% Q( r, v$ t2 F
部分文件名(title.txt)
; p/ e! L! g! ^0 I1 L# t
: F3 U' ~; a2 Y4 `/ ?& EB12-B51-58
' s% p5 k4 _# |" \! o7 {B12-B53-58+ D* M9 {. D$ ]
B12-B55-59
( l4 r6 b6 r$ ]8 QB12-B55-62& w) b0 k& n$ U, G+ ^  U
B12-B55-64
7 Y5 }; i% K6 y6 T! wB12-B57-51; n% X& A1 V) K4 E+ m
B14-B50-44- z1 V/ e6 J. ~+ P6 `0 r
B14-B50-483 v# d1 M" X, f- Y2 s7 {
B14-B51-46
, \, ?) u0 X! ^% L( _& |% a9 _% k& @B14-B52-496 Y6 I3 |6 x& W  K6 F
B14-B54-48) o4 x" W' Z  p% B( h0 `
B14-B54-530 q9 \6 M, ~0 y0 i6 Y/ v2 e; {
B14-B54-74# l9 i+ O& Y2 |& \  B7 I8 ?
B14-B55-52
7 o2 U7 N! d3 d1 Y8 vB14-B55-56
9 }9 I! [. y/ ]# S4 t; PB14-B55-60# S; M: g$ j$ t7 o
B14-B55-63
: Z0 U* d5 w- L7 l% e2 Z- pB14-B55-658 ?: z0 e9 O8 a+ x4 {* L$ h0 d/ W
B14-B55-67
8 @5 G  i2 P, V9 Q2 H" EB14-B55-69
6 V* }3 ]# d- }4 G. Z. CB14-B55-75
! x& B' k* Q' y8 L* h0 L4 ^3 B; ]" ZB14-B56-498 W: x5 r! t$ b1 C' s; y6 N
B14-B56-53+ \$ P1 k. W+ N( j; n3 B
B14-B56-70  ^1 N8 d1 z# p: h

) B( ^+ {* [% ^1 f! X…………
! X! X7 _. n* G* X; d
8 Z2 d+ z( \6 P( `! X
0 f( l! r" O3 q. ^( I* s! o. Z$ H& h& e, n( ~) ^3 n
function [efc_dpt,efc_poro,efc_perm,efc_ratio]=numprocessor(file)! C' }4 N; c- w  l
%读入数据,进行处理,输出结果
6 z2 p3 D1 _$ A    matdat=load(file);- G2 ]5 X3 D, t) j$ j. G4 p; S
    [n,l]=size(matdat);' o2 X, @; J7 }# l
   : O8 Z& c" W. @+ q* J
    efc_dpt=0;! f+ A8 p: g* T+ S* h
    efc_perm=0;- }& J) S# g2 y) E1 ~9 u$ X4 q( e
    efc_poro=0;+ A& m) y; D+ z1 K. K
    efc_ratio=0;
) a' m' ]; Y9 Q( g2 q   
6 f" y' A( J- f# K    for i=1:n-1
7 Y- U9 M( W; c8 {" h        matdat(i,1)=matdat(i+1,1)-matdat(i,1);
1 F- T4 g6 f# ~1 L3 e7 \5 P/ t    end
  {& l3 J$ R' |+ G& q   
- W& S8 s4 @& U+ U7 Z    for i=1:n-1
. n4 D- X* Z& m# x# H; P        if matdat(i,3)==1/ A8 k, w. V! \' _
            efc_dpt=efc_dpt+matdat(i,1);! ?  T- @9 s; H9 K$ J( T. D; \
            efc_perm=efc_perm+matdat(i,5)*matdat(i,1);
; m2 Z0 t$ \* {& V$ i) o            efc_poro=efc_poro+matdat(i,4)*matdat(i,1);, }/ ]  p3 z5 G' M& h' d
            if matdat(i,2)==0# K  o1 `8 \* l+ M; K
                efc_ratio=efc_ratio+matdat(i,1);
3 S8 L2 V& a3 \            end1 I& L1 L9 V! n9 r$ v+ o
        end
4 f& K; s; }7 b    end
3 V) T% Y  G- `8 [5 h    if efc_dpt==03 ?9 E' P, x/ _7 y9 M/ Q$ Z
        efc_perm=0;3 ]; @: B$ x( ^8 u6 H; T1 @
        efc_poro=0;9 k1 @' K! I+ p0 [6 D# ?& s
        efc_ratio=0;9 Z- _$ U) i2 Q$ V' K( F/ J( r# N

0 ]8 R# Y9 q: R, j" F) V6 R    else
( x" `, k. g, l0 b        efc_perm=efc_perm/efc_dpt;
! Q8 L0 R& P( l" i: G        efc_poro=efc_poro/efc_dpt;  U7 D7 N1 G" Y
        efc_ratio=efc_ratio/efc_dpt;       9 s9 D2 Q" y- y' S- @
end- I/ x% B1 K1 S5 U6 W* B

5 k8 x2 M  Z) L : S/ v6 w& C0 `" W

) e  ^5 p4 z( [0 G: c. o$ w井的数据
; D$ ?1 h* t4 i0 ?: d0 D% f# j0 c0 O5 {- E) p
1865.2 3 0 0 06 \8 {  K( d; a0 ?
1867.8 0 1 6.8 0.1
" z; [5 o: d5 m' h1 [+ t1874.4 3 0 0 0
8 G' K( O2 R- V+ u/ P5 S1885 1 1 3.3 0.02
+ _5 C% [7 ~: Q! o" I. g7 z# |4 H6 ^1888.8 3 0 0 0
! M. V- R+ F3 @" ^' T) v1891.8 0 1 2.3 0.03
6 [. _* ^) T; e) t. R1897 3 0 0 0
" {( ~9 N$ m7 d/ W2 ^, I& i1898 0 1 2.3 0.039 N: u* b4 R( @& G
1903 3 0 0 0
0 V- L8 y8 G2 ~7 h) d/ \* F1906.2 0 1 2.3 0.03" F2 j% v* Y8 [' [- V9 l: D
1911 3 0 0 0
. f- x/ f+ g' F/ x! j2 @1914.6 1 1 3.4 0.210 Y: j- E5 `6 p& p5 E
1919 3 0 0 0
2 M0 D: j5 G; z, j/ i+ q& N# |& ?5 `. Z: d" z. w. _
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
. X: P+ D0 z7 v/ k; G& q% v+ d" o# C- n6 J' D$ a" {9 d/ O
多数据文件批处理的一个技巧) r; [, G9 D: ]0 j0 I+ d) v

* A3 w2 b$ Z8 V( z% L4 e' l数据处理过程中,很多时候需要对大批的数据文件进行循环处理。如果文件名是有规律的,那么正常的方法尚可处理,但是碰到文件名没什么规律,或规律不强时,表示出这些文件按名就是一件很头疼的事了。假设现有以下文件需要进行循环处理,以供参考。
9 ], r% O5 ]9 {- m) O& d) B& @  u. {
- T9 }1 O& s% }5 P9 O1、MATLAB % }8 G1 v  @( u2 `' Z8 e! C
clc;clear;
  e5 U, u0 c( L# a%--将'e:/POP/data/'下的文件信息写入构架数组file_structure
9 Y$ T5 \6 S4 C2 ^file_structure = dir('e:/POP/data/');
; [- W4 Q1 Q5 `; N! W%--file_structure.name下是'e:/POP/data/'内的文件名. C6 m3 I$ T% \
for i=3:size(file_structure)      
; d" m+ J& @% O/ E3 B% S: ?0 N7 U$ t9 K+ T4 k1 T
  filename = strcat('e:/POP/data/',file_structure(i).name)                 % stract 连接字符串/ r' y, l) k+ x, `2 s
  f=netcdf(filename,'nowrite');  3 d- ?7 u+ _; K/ j7 M/ |7 W
  a=['在此对f及里面的变量操作...']  9 q7 X9 Y: A! ~$ |. ^5 U8 V$ a3 t% E
  close(f)
  p4 g+ m% L) g6 Z; V* Oend7 x7 @  G# W% P

" ]7 [/ {  m+ @" T1 I
/ D% y4 ?5 J% _3 ~ -----------------------------------------------------) `) w/ m' ]$ M  s
怎么用matlab读取多个数据文件?/ Q- Q" l8 ~& r* T% W  J1 x% ^4 \6 E

, U% b5 n( c* T) |2 g  ?# Y" ~5 J4 ~8 Q- E6 J
1:如果文件名规则& j+ F, y. K- O0 E3 a3 S0 U

  _, q! r. o$ k3 p2 \
& F* G* H: j/ d& P4 P$ r( d% 文件目录:
  s& ~$ s: a8 `% |1 i) m5 l) \  g
9 h6 `8 J. H3 q4 |3 X* X' D
my_dir=' /home/my_calculation1/test1/';
/ R, m1 _% q: L, m1 m& k. f6 v, y: E
' {% U2 ~* |& E' _3 g
% v# o0 j6 r6 e) q9 d, \1 M' R

/ K5 P* \- B$ J# g* L7 i
" C; }9 Y- o7 h3 ~, ]2 P%文件名前缀
# e- @- x! E) [2 q, Y6 P! E% U9 {4 _3 x
my_pre_T='test';6 k  s+ R+ v& ?
%文件数目 fnum =1:32; %Read files for i=1:length(fnum) filename = [my_dir,prefix_T1 num2str(fnum(i))]; 然后dlmread,fopen等操作就不需详写了!与单个文件一样。# a- l1 e/ L  `) D: S# N3 s  O9 K
end
* A* O3 E3 }5 I8 P
3 c# |- H4 Z2 ]1 F
- @0 h' h1 m  i, h' v4 V
$ T. l/ ^5 y1 P& |. Z
, q2 ~4 ]) g: t5 \& |& [0 v. ]: q
8 d. P8 M1 Y: _- }2:文件夹数据批量读取的问题,命名无规则5 D1 S. G3 t# d% v
在windows下很简单,ls函数便可以得到一个包含所有文件名的字符矩阵。可惜在unix下得到的是个字符行向量。当然,处理下也可以批量读取数据文件,不过不想再写了。因为另外一个函数也可以实现批量处理数据文件的功能。即dir函数
6 ~+ _0 ^5 \/ e  q0 T; h) g6 j4 F! m! ?) {( k  t+ j1 U% I: ^
LS displays the results of the 'ls' command on UNIX. - ~5 c1 o- K  g) J3 C

3 q6 }: V# x! b9 s3 K* c: l/ j' h& f6 [) ^

, |/ t) Y7 j8 e$ r' }; ]5 A* L# lOn UNIX, LS returns acharacter row vectorof filenames separated by tab and space characters.
6 h, ~5 |+ y8 a$ _( _, t6 _5 Q7 M; n9 n$ w  v; V

# A  G& C4 W: `& E2 e8 n3 E' {' t5 J) m5 q' \  p
7 F& u. s0 H* \9 t# M# j
1 ]8 D5 @6 i) w. w, ~) M! D
4 L, s) L6 e2 z; U0 s
# U3 F; d) |+ X3 A
假定.dat文件在/home/my_calculation1/test1/ 文件夹下,文件名符合matlab变量名的命名规则,要读取第二列的浮点数字为数组并以文件名为变量名。3 C2 z  f( d' d% a! Y
0 E: [9 f. j. J# p1 [$ X8 z

9 K4 |; n4 Q, j$ o9 y, \+ B5 i2 d# R; a1 q* Z
mydir='/home/my_calculation1/test1/';
7 q6 _/ O" r7 m3 U4 ?
7 ?6 _. i1 Q$ [2 n. W4 Utemp1=dir([mydir,'*.dat']);
! R/ z- Z5 [% {* F$ [  I3 @' {0 b  s0 O+ p

( Z( V0 @6 K; Q: [; e# M" J# F, k. H; `

9 Y% j# }* j* o' Ctemp=dlmread(filename,'',0,1);2 z7 O3 `* ~3 y$ F. V: Q+ M
. Z! \( a5 i3 A8 r3 r5 T4 n

/ x- d& K7 ]% ^/ R2 g1 L4 t
- {& s; s; L& [$ l! x
8 B& a& _) R: E) N1 Y+ s- z* X0 G; I# \0 ]" x
9 U! K# I. @+ H4 \
1、利用dir(外层文件夹)获取子文件夹;# |# E8 N7 d: @) r3 j' b5 t& B
2、利用dir(子文件夹)获取子文件夹中的文件信息;7 k. Y3 B9 \* ~$ P' K* I
3、遍历文件,做处理;7 Z4 @* l! W0 O4 t; K
对于遍历文件夹,也可以使用mathworks网站上的函数dirr轻松搞定。% I+ B: k: {$ ?
9 [% H# N# C8 q& j
' Q/ J- @* \, w) k; Y

- b) X7 a' G1 u( V* D注意:如果直接使用 temp1=dir(mydir]);读取文件夹下所有文件,应当从第三个开始才是目录下的文件。 另外,使用dir还可以遍历一个文件夹下的所有子文件夹。
3 a8 d8 D! h2 x8 G1 Heval_r([temp1(i1).name(1:end-4),'=temp;'])
1 y8 N& I' }; o$ h! x4 o
+ f/ D% c" U; C; Y, T1 X2 U; H+ O$ o9 f9 h+ u9 d  G. D
dlmread,fopen等操作就不需详写了!与单个文件一样。
) z7 h/ a; m9 W4 h% ]8 ~6 {+ Rend# D5 p0 m9 o$ {1 N
, m2 o9 H. t5 Q5 }2 r! k

/ X( b- \1 q+ U6 w4 ]
/ i9 y1 z( V  {7 q
; o7 h1 v& `# Q( s3 F" x% ~2 [$ r6 u$ P: _2 \& o  P6 z5 e

" J* q% o  |, u8 bnum_temp1=length(temp1);
1 l' {: A+ K7 G6 T9 c+ H# g1 {. ]5 B, H5 s( G: h& r+ I. d
! `. Z/ w' X& G6 ?' Z/ t' d

9 K, W' G' S2 t( x; s# M4 a7 T* }  [1 _5 f, r7 J. _
for i1=1:num_temp1
% s' \6 W4 ?% M3 h- m
- Q- t5 I# e3 Q; ]# S, O1 U
8 K  B2 [6 ^  u' L7 \filename=[mydir,temp1(i1).name];  {) _3 n0 Q0 Z8 \

# Q8 g7 G, C: r6 z' B- R! t- N( ~

3 g" R, s: F  E3 C: p* O/ f9 S- b+ h1 D3 [7 U( M$ s" V# C- n$ i$ q
5 [2 q& ?" Y5 V- g- A$ G
% G/ K" L3 r" ^6 @7 T

. @- P2 N4 ?- P+ [4 ~0 n7 g# y5 N: b
; ?3 X+ [0 y1 C! C! M
* ?, X9 a6 R, M% x4 O3 t' S' j# |0 N1 G( u9 g
" _! d& \' d0 j$ B0 G
现在有一个文件夹里面有50个的.dat文件.每个文件大概三万行.两列,第一列是字符串,第二列是浮点数字. 批量读取这个文件夹里的.dat文件的第二列。读取完以后的数组变量名就是.dat的文件名+ n9 n) Z; j& f1 k" p
0 L+ ~, I4 l# q. w" i
$ ^( C& m: Q$ A( `

1 F5 g0 s. A6 i: X) hOn Windows, LS returns anm-by-n character array of filenames, where m is the number of filenames and n is the number of characters in the longest filename found. Filenames shorter than n characters are padded with space characters.
% b/ [! ^" \0 ~, {" j
; n7 V) M0 a2 ^% W" X
: N& m9 n* X3 _6 X下面介绍下使用dir函数读取文件夹中文件的程序
3 U0 |; F' s9 {3 B# H# c) s1 k& _) k. c. h2 N8 Z9 Q

3 Z2 t2 s1 s1 Y& I' F4 K  l2.1 读取所有扩展名为.dat的数据文件,并进行处理。
; [' o# J1 z; Y: n% E. B* ?2 U8 Z* e% K4 D2 p+ v9 a; _$ Z4 H

. z' h) Q3 i- s( G, amatlab中,也可以使用通配符的。下面就来展示下:
作者: wu68aq    时间: 2020-3-17 17:50
这个方法不错。
作者: ExxNEN    时间: 2020-3-18 18:23
matlab批量读入数据文件的方法




欢迎光临 EDA365电子论坛网 (https://bbs.eda365.com/) Powered by Discuz! X3.2