|
|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
6 W5 R6 u0 E6 `( j L1 d
textscan 的用法与 fscanf 类似,建议先将 fscanf 的用法弄清楚再来看 textscan。$ m: o+ s" F: @; [; G
' T( J1 w' l7 d' R7 I, z
, R. g. e; g8 E# `6 \. L
textscan 常见用法:
; d/ ?4 l% t9 f* z$ F# I+ l3 O; g- C = textscan(fileID,formatSpec)
- C = textscan(fileID,formatSpec,N)
1 f5 Q( o( F; \6 H& m" p $ t. u- q6 Q9 Z b8 R& t- k
* v5 |3 R; d. F6 g同 fscanf 一样,fileID 为文件标识符,formatSpec 为格式字符串。N 则是重复匹配formatSpec 的次数。
$ F1 L! a' z% a$ o! \) k; a. w与 fscanf 不同的是, textscan 将每个与 formatSpec 转义说明符匹配出来的数据都用一个元胞进行存储。并且 textscan 有很多选项提供,比如 ’Headerlines’ ,可以指定跳过文件的前n行; ’Delimiter’ 可以指定分隔符等等。
9 y4 n4 h( F! c3 z- Q: C# A; n W$ q+ A- s
2 S$ o1 r: h: m3 d例:文本文件test.txt包含以下数据:6 Z' e, v0 E2 e
W& p3 ^* Y) P; C9 g% L16。2。3。13. W2 J! b: T7 y3 n& L) s1 a" ?& V
5。11。10。86 l8 I% m8 m/ R3 H. p! n
9。7。6。12
% Z6 D( v5 \0 [4。14。15。1% G) g' |* Q8 |: M
* O- N# R7 L, {6 l
6 w0 O& m, a( y8 k; a8 x
- fid = fopen('F:\test.txt');
- formatSpec = '%d'
- A = textscan(fid,formatSpec,'delimiter','。'); %指定’。’为分隔符,如果不指定分隔符的话,就需要把formatSpec写成'%d。%d。%d。%d' 。
- fclose(fid);
- celldisp(A)
- A{1} =
- 16
- 2
- 3
- 13
- 5
- 11
- 10
- 8
- 9
- 7
- 6
- 12
- 4
- 14
- 15
- 1
1 ^( t' m; \8 T# e3 Q' a
T% B( ~3 ^- a- Q0 q% x |
|