-
MATLAB
对于文本文件
(txt)
数据读取的技巧总结,
ZT
一
.
基本知识:
-------------------------------------------------
-
转
--------------------------
--------------------------
1.
二进制文件与文本文件的区别:
将文件看作是由一个一个字节
(byte)
组成的,
那么文本文件中的每个字节
的最高位都是
0
,
也就是说文本文件使
用了一个字节中的七位来表示所有的信息,
而二进制文件则是将字节中
< br>的所有位都用上了。这就是
两者的区别;接着,第二个
问题就是文件按照文本方式或者二
进制方式打开,
两者会有什么
不同呢?其实不管是二进制文件也好,
还是文本文件也好,
都<
/p>
是一连串的
0
和
1
,
但是打开方式不同,
对于这些
0
和
1
的处理也就不同。
如果按照文本方
式打开,在打
开的时候会进行
translate
,将每个字节转换成
ASCII
码,而以按照二进制方式
打开的
话,则不会进行任何的
translate
;最后就是文本文件
和二进制文件在编辑的时候,使
用的方式也是不同的。譬如,你在记
事本中进行文本编辑的时候,你进行编辑的最小单位
是字
节
(byte)
;而对二进制文件进行编辑的话,最小单位则是
位
(bit)
,当然我们都不会直接
通
过手
工的方式对二进制文件进行编辑了。
从文件编码的方式来看,文件可分为
ASCII
码文件和二进制码文件两种:
ASCII
文件也称为文本文件,这种文件在磁盘中存放时每个字符对应一个字节,用于存放对
应的
ASCII
码。例如,数
5678
的存储形式为:
ASCII
码:
00110101 00110110 00110111 00111000
↓
↓
↓
↓
十进制码:
5
6
7
8
<
/p>
共占用
4
个字节。
ASCII
码文件可在屏幕上按字符显示,
例如源程序文件就
是
ASCII
文件,
用
DOS
命令
TYPE
可显示文
件的内容。由于是按字符显示,因此能读懂文件内容。
p>
二进制文件是按二进制的编码方式来存放文件的。例如,数
5678
的存储形式为:
00010110
00101110
只占二个字节。二进制文件虽然也可在屏幕
上显示,但其内容无法读懂。
C
系统
在
处理这些文件时,并不区分类型,都看成是字符流,按字节进行处理。输入输出字
p>
符流
的开始和结束只由程序控制而不受物理符号
(
如回车符
)
的控制。
因此也把这种文件称作
“
流式
文件
”
。
2.
文
本模式
(textmode)
和二进制模式
(binarymode)
有什么区别
?
< br>
流可以分为两种类型:<
/p>
文本流和二进制流。
文本流是解释性的,
最长可达
255
个字符,其中
回车
p>
/
换行将被转换为换行符
“
n”
,
(
如果以
文本
方式打开一
个文件,
那么在读字符
的时候,
系统会把所有的
序列转成
p>
,
在写入时把
p>
转成
)
。
二进制流是非解释性的,
一次处理一个字符,并且不转换字符。
注:
n<
/p>
一般会操作系统被翻译成
行的结束
,即
LF(Line-Feed)
r
会被翻译成
回车
p>
,即
CR(Cariage-
Return)
对于文本文件的新行,在
UNIX
上,一般用
n(LF)
来表示,
< br>Mac
上用
r(CR)
来表示,
1
/
19
Windows
上是用
nr(CR-
LF)
来表示。
通常,
文本流用来读写标准的文本文
件,
或者将字符输出到屏幕或打印机,
或者接受键盘的
输入;而二进制流用来读写二进制文件
(
例如图
形或字处理文档
)
,或者
读取鼠标输入,或
者读写调制解调器。
如果用文本
方式打开二进制文件,
会把
“0D 0A”
自动变换成
“
n”
来存在内
存中。写入的时候反向处理。而二进制方式打开的话,就不会有这个过
程。但是,
Unicode/UTF/UCS
格式的文
件,必须用二进制方式打开和读写。
------------------------------------------
--------------------------------------------------
-------------
上述基础其实大可以略过,简言之,对用户来说:在
matlab
中存储成为二进制还是文本文
< br>件取决于
fopen
的方式,如果用
wt
,则存储为文本文件,这样用记事本打开就可以正常显
示了;如果用
w
则存储为二进制文件,这样用记事本打开会出现
小黑方块,要正常显示的
话,可以用写字板或
UltraEdi
t
等工具打开。
二
. Matlab
的
I/O
文件操作使用技巧和总结
:
1. Matlab
p>
支持的
I/O
文件(对应
< br>“
取
/
存
”
操作)类型:
(所有文件
I/O<
/p>
程序不需要特殊的工具
箱)
/support/tech-notes/1100/
(注:
从上表可以看到,
matlab
不支持
doc
格式的文档存取
(因为
doc
文档包
含很多格式控
制符)
,请改用
txt<
/p>
或者
dat
格式)
2. Matlab
的
I/O
文件指南:
/support/tech-notes/1600/
以下是部分对应的中文译文:
---
--------------------------------------------------
---------
转
------------------
----------------------
本技术支持指南主要处理:
ASCII, binary,
and MAT files.
要得到
MATLAB
中可用来读写各种文件格式的完全函数列表,可以键入以下命令:
help iofun
MATLA
B
中有两种文件
I/O
程序:
high level and low level.
High level routines:
包括现成的函数
,可以用来读写特殊格式的数据,并且只需要少量的编
程。
Low level routines:
可以更加灵活的完成相对特殊的任务,需要较多的额外编程。
High
level
routines
包括现成的函数,可以用来读写特殊格式的
数据,并且只需要少量的编
程。
2
/
19
举个例子,如果你有一个包含数值和字母的文本文件(
text
file
)想导入
MATLAB
,你可
以调
用一些
low level routines
自己写一个函数,或者是简单的用
TEXTREAD
函数。
使用
high
level
routines
的关键是:文件必须是相似的(
homogeneous
)
,换句话说,文件必
须有一致的格式。
下面的段落描述一些
high
level file I/O routines
并给出一些例子帮助理解概
念。
LOAD/SAVE
主要的
high level
file I/O routines
是
LOAD
和
SAVE
函数。
LOAD
可以读
MAT-file
data
或者用空格间隔的格式相似的
ASCII data.
SAVE
可以将
MATLAB
变量写入
MAT-
file
格式或者空格间隔的
ASCII data
。
大多数情况下,
语法相当简单。
下面的例子用到数
值由空格间隔的
ASCII
file sample_
:
1 5 4 16 8
5 43
2 6 8
6 8 4 32 1
90 7 8 7 6
5 9 81 2 3
Example:
用
LOAD and SAVE
读写数据
CODE:
% Load the file to the
matrix, M :
M = load('sample_')
% Add 5 to M :
M = M +5
% Save M to a .mat file called
'sample_file_':
save sample_file_plus5
M
% Save M to an ASCII .txt
file called 'sample_file_' :
save
sample_file_ M -ascii
3
/
19
UIGETFILE/UIPUTFILE
UIGETFILE/UIPUTFILE
< br>是基于图形用户界面(
GUI
)的。会弹出对话框,列出
当前目录的文件
和目录,提示你选择一个文件。
UIGETFI
LE
让你选择一个文件来写(类似
Windows
‘
另存为
’
选项?)
p>
。用
UIGETFILE
,可以选择已存在
的文件改写,也可以输入新的文件名。两个函数的
返回值是所选文件名和路径。
Example:
用
UIGETFILE
从当前目录选择一个
M-file
CODE:
% This
command lists all the M-files in the current
directory and
% returns the name and
path of the selected file
[fname,pname] = uigetfile('*.m','Sample
Dialog Box')
注意
: UIGETFILE
一次只能选择一个文件。
UIIMPORT/IMPORTDATA
UIIMPORT
是一个功能强大,易于使用的基于
GUI
的
high level routine
,用于读
complex data
files
。文件也必须是
homogeneo
us
。
I
MPORTDATA
形成
UIIMPORT
的功能,
不打开
GUI
。
可以将
IMPORTDATA
用于函数或者脚本
中,
因为在函数或者脚本中基于
GUI
的文件导入机制并不理想。
下面的例子用到包含几行文件头
和文
本、数值数据的文件
'sample_'
:
4
/
19
This is a file
header.
This is file is an
example.
col1 col2 col3
col4
A 1 4 612.000
B 1 4 613.000
C 1 4 614.000
D
1 4 615.000
Example: Using
IMPORTDATA to read in a file with headers, text,
and numeric data
CODE:
% This reads in the file
'sample_' and creates a
% structure D
that contains both data and text data.
% Note the IMPORTDATA command specifies
a white space
% as the
delimiter of the file, but IMPORTDATA can usually
% detect this on its own
D =
importdata('sample_','') %
原文有误?
D = importdata('sample_')
p>
可以通过访问结构
D
的数据和文本域,来看
结构
D
中的真实值,例如输入:
data =
text = ta
可以用
UIIMPORT
读同一个文件并得到同样
的结构
.
5
/
19
注意
:
对于
ASCII data,
你必须检验导入向导正确的识别了列分隔符。
TEXTREAD/STRREAD
TEXTREAD
是一个强大的动态
high level routine
p>
,
设计用来读
ASCII
< br>格式的文本和
/
或数值数据
文件
。
STRREAD
除是从字符串而不是文件读以外,类似于
p>
TEXTREAD
。
两个函数可以用许多参数来改变其具体的工作方式,
他们返回读入指定输出的数据。
他们有
效的提供
给你一个
“
两全其美
”
的方法,因为他们可以用一个命令读入混合的
ASC
II
和数值数据(
high
level
routines
的做法
)
,
并且你可以改变他们以匹配你特定的应用
< br>(如同
low level routines
做到的)
。
例子:
CODE:
Example 1:
Using TEXTREAD to read in an entire file into a
cell array
% This command
reads in the file fft.m into the cell array, file
file = textread
('fft.m','%s','delimiter','n','whitespace','');
CODE:
Example 2:
Using STRREAD to read the words in a line
% This command uses the
cell array created in Example 1 to
% read in each word of line 28 in
'file' to a cell array, words
words =
strread(file{28},'%s','delimiter','')
CODE:
6
/
19
Example 3: Using TEXTREAD
to read in text and numeric data from a file with
headers
% This command
skips the 2 header lines at the top of the file
% and reads in each column to the 4
specified outputs
[c1 c2 c3
c4] = textread('sample_','%s %s %s
%s','headerlines',2)
CODE:
Example 4: Using TEXTREAD
to read in specific rows of text and numeric data
from a file
% This command
reads in rows B and C of the file. The
'headerlines'
% property is used to
move down to the desired starting row and the
% read operation is
performed 2 times
[c1 c2 c3 c4] = textread('sample_',...
'%s %s %s
%s',2,'headerlines',4)
CODE:
Example
5:
Using
TEXTREAD
to
read
in
only
the
numeric
data
from
a
file
containing
text
and
numbers
% This command reads in only the
numeric data in the file. The
%
'headerlines' property is used to move down to the
first row
% of interest and
the first column of text is ignored with the
% '*' operator
[c2 c3 c4] =
textread('sample_','%*s %d %d %f','headerlines',3)
DLMREAD/DLMWRITE/CSVREAD
7
/
19
DLMREAD
和
DLMWRITE
函数能够读写分隔的
ASCII
data
,而不是用
low level
routines
。他们
比
low
level
routines
容
p>
易
使
用
,
Low
level
routines
用
几
行
代
码
实
现
的
功
能
可
以
用
DLMREAD/DLMWRITE
简化成一行。
CSVREAD
用来读分隔符是逗号的文件,是
DLMREAD
的特殊情况。当
读空格和
Tab
分隔的电
子数据表文件
时,
DLMREAD
特别有用。以
's
ample_'
为例:
CODE:
Example 1:
Using DLMREAD to read in a file with headers,
text, and numeric data
% This reads in the file 'sample_' and
creates a matrix, D,
% with the numeric
data this command specifies a white space as the
% delimiter of the file
D = dlmread('sample_','')
CODE:
Example 2:
Using DLMREAD to extract the first 3 columns of
the last 3 rows
% This
reads in the first 3 columns of the last 3 rows of
% the data file 'sample_'into the
matrix, D_partial.
%
读文件
'sample_'
前
3
列后
3
行,到矩阵
D_partial.
D_partial = dlmread('sample_','',[2 0 4
2])
CODE:
8
/
19
Example 3:
Using DLMWRITE to write a comma delimited file
% This creates a file
called '' that consists of
% the first 3 columns of the last 3
rows of data where each
% element is
separated by a comma
dlmwrite('',D_partial,',')
注意
:
保证
DLMREAD and DLMWRITE
指定范围的指标从
0
开始,而不是从
1
开始。
WK1READ/WK1WRITE
WK1READ
用来读
Lotus123
电子数据表
文件的数据;
WK1WRITE
用来写矩阵到
< br>Lotus123
电
子数据表文件。
XLSREAD
XLSREAD
< br>用来读
Excel
的数值和文本数据。
< br>
-----------------------
--------------------------------------------------
--------------------------------
三
.
具体例子分析:
< br>Matlab
网站用两个例子非常详尽地介绍了各个命令的基本用法,实际中,面
对手头上的数
据,如何选用合适的命令呢?以下结合几个示例给出一些总结,大家举一反
三就可以了:
1.
纯数据(列数相同)
:
源文件:
CODE:
0 3866.162 2198.938 141.140
1 3741.139 2208.475 141.252
2 3866.200 2198.936 141.156
3 3678.048 2199.191 141.230
4 3685.453 2213.726 141.261
9
/
19
-
-
-
-
-
-
-
-
-
上一篇:HighCharts 中文API 文档
下一篇:毕业后如何在校外下载期刊全文