Matlab 的数据文件读取
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Matlab 的数据文件I/ O 的实现方法
0 引言
Matlab 是一个功能强大的计算机辅助设计软件,被称为“草稿纸式的科学计算语言”,它具有强大的符号、数值计算、矩阵运算以及图形显示的功能,它被广泛地应用在数字信号处理、自动控制、图像处理、神经网络、优化设计、模糊逻辑、仿真、小波分析等方面。
Matlab 应用中常需要实现Matlab 与其他应用程序的数据共享,即需将数据文件读入Matlab 进行有效的数据处理,然后将Matlab 处理好的数据保存为数据文件,以便其他应用程序所使用。
Matlab 支持多种文件格式的输入和输出,如.dat 、.txt 、.mat 、.bmp 等。
在实际进行数据文件的I/ O 时常出现错误,本文详细讨论Matlab 中数据文件I/ O 的实现方法,并给出每种方法的具体实例。
所有的数据文件I/ O 操作文件都在Matlab 中,无需安装专门的工具箱。
数据文件I/ O 程序可分为两种基本类型:低级和高级。
高级包括一些专门的函数以支持特定格式的读和写,相对而言低级则比较灵活。
1 高级数据文件I/ O 方式命令
高级数据文件I/ O 命令主要有Load 和Save 。
Save 可以将Matlab 变量以ASCII 码格式或MAT存储, Load 可以读ASCII 码格式的变量或MAT 文件到Mat2lab 工作区。
这两条命令的使用比较简单, (为便于读者可直接将以下文本输入计算机运行,注释采用英文) 如:
%This file demonstrates the behavior of LOAD and SAVE
clear all%Clear all variables
x = 5 ;x2 = 10 ; %Create x and x2
whos %To ,display the variables in workspace
pause %Pause so as to observe
save xdata x%Save only x into xdata. mat
clear all%Clear all variables
load xdata%Load xdata. mat
whos %Note that x is back
pause
x2 = 10 ; %Recreate x2
whos
pause
save xdata x x2%Now save using a wild card
clear all%Clear all variables
load xdata%Load xdata. mat
whos
使用Load 和Save 还可以实现一次读多个文件或将数据存入多个文件,以Save 命令为例(Load 类似) ,程序如下:
a = rand(6) ; %make a (6 3 6) random number
filename = ['a1.txt';'a2.txt';'a3.txt';'a4.txt';'a5.txt';'a6.txt'; ] ; %list your file names
for i=i:1ength(a)
temp = a(:,i) ;
save (filename (i , :) ,'temp''-ascii') ;
%save to the specified file
end
dlmread 和teatread 函数可以实现读入格式化的ASCII 码数据而不使用低级命令。
这些函数比低级命令易于使用,作为低级命令需要多条语句来实现,而这两个命令可以简化为一条语句。
如果你的数据文件是数字的但中间以定界符分隔(或你想写这样的文件) dlmread 可以把数据读入一个矩阵而忽略定界符,DLMWRITE 则允许你写这样格式的文件。
当你想读入Excel 文件,数据可能以空格或Tab定界,DLMREAD 特别有用,例如你将以Tab 界定的Excel 文件保存为mydata. txt ,可以将其读入矩阵,语句为:
M= dlmread ('mydata. txt','\ t') ; % \ tindicatesa tab
TEATREAD 函数允许你读入ASCII 码和数值混合的数据文件,详细说明可查在线帮助help teatread ,
假设‘mydata. dat’文件包含以下内容:
Name Type Score Y Y/N
Sally Type112.3445Yes
Joe Type223.5460No
Bill Typel34.9012No
你想将每一列分别读入单独的变量,使用命令:
[names,type,score,y,answer]=teatread ('mydata.dat','%s %s %f %d %s','headerlines',1) ;
2 使用低级命令读ASCII 码格式文件
Matlab 的低级I/ O 命令跟C 语言很类似,以下几个常用的命令可用于读入ASCII 文件: FOPEN —打开文件; FSCANF —从文件读格式化数据; FPRINTF —写格式化数据到文件; FGETL —从文件读入一行;FTELL —返回当前文件位置; FSEEK—设置文件指针位置;FCLOSE —关闭文件指针。
值得注意的是必须先使用fopen 打开文件,返回一指针指向文件开头,当使用fscanf 和fprint 时指针会跟随移动。
必须注意关闭文件,否则再次打开时会出现错误。
以下程序演示怎样使用Matlab 低级I/O 命令读写一个简单的ASCII 码文件,程序如下:
fid =fopen('square mat. txt','wt') ; %open the file
fprintf (fid ,'%s \ n','This is a square matrix' ) ;
%write the header
fprintf (fid ,'%i \ t %i \ t %i \ n','[1 2 3 ;4 5 6 ;7 8 9 ]') ;
%write data to file
fclose (fid) ; %close file
disp ('Write of file is successful . . . press spacebar to contin2ue')
pause
%read the file back in
fid =fopen('square mat. txt','rt') ; %open the file
title =fget1(fid) ; %read in the header
[ data ,count ] =fscanf (fid ,'%i',[3 ,3 ]) ; %read in data
fclose (fid) ; %close the file
如果你试图用这些低级命令读带有标题、字符和数字混合或包含Nan 和Inf 的文件时,将会变得很困难,以读入字符和数据混合文件为例,假设mydata2.dat 包含:
FPl 2.345.666.777.88
Cp6 2.445.557.777.88
RTPl2 5.553.336.665.55
程序如下:
%Find out number of rows in
filer = 0 ;x = 0 ;
%Open Data File
fid =fopen('mydata2. dat','rt') ;
%Loop through data file until we get a21 indicating EOF
while x= =-1
x =fget(fid) ;x = x+ 1 ;
end
x= x-1 ;
disp (['Number of rows =' num2str(x)]) ;
frewind(fid) ;
for I = 1 :x
name =fscanf (fid ,'%s',1) ;
%Filter out string at beginning of line
num =fscanf (fid ,'%f %f %f %f \ n') ;
%Read in numbers
if (i = = 1) names = name ; %Add lst text string
result = num; %Add lst row
else names = str2mat (names ,name) ; %Add next string
result = [ result ;num] ; %Add additional rows
end
end
fclose (fid) ;disp ('') ;
disp ('Data read in successfully :') ; disp (result) ; disp ('')
disp ('Strings read in successfu11y :') ;disp (names) ; disp ('')以下程序可以实现将数值读入,而忽略字符串,相对前面的程序简单多了。
fid =fopen('mydata2. dat','r') ;
b =fscanf (fid ,'% 3 s %g %g %g %g') ;
b = [ reshape (b ,4 ,3) ] ;
fclose (fid) ;
3 低级命令读二进制数据文件
跟低级命令读ASCII 码格式文件过程类似,低级命令可以读二进制数据文件。
命令有:fopen —打开文件、fread —二进制文件读、fwrite —二进制文件写、ftell —返回文件指针当前位置、fseek—设置文件位置标志、fclose —关闭文件位置,与前面的读ASCII 格式差别主要是fread 和fwrite 代替了fscanf 和fprintf。
应该注意的是你必须清楚待读文件的一些信息,如每个数据占几个字节,总共占多少
个字节等。
例如:
%This m2file is a short example of how to read and write a bina2ry file
%in MATLAB using low level routines
%write file
fid =fopen('square mat. bin','wb')
%open file in binary write mode
fwrite (fid ,'This is a square matrix','char') ;
%insert a header
fwrite (fid ,'[1 2 3 ;4 5 6 ;7 8 9 ]','int32') ; %write data to file
fclose (fid) ; %close file
%read in the same file
fid =fopen('square mat. bin','rb') %open file
bintitle =fread(fid ,23 ,'char') ; %read in the header
title = char ('bintitle')
data =fread(fid ,[3 inf ] ,'int32') %read in the data
data= transpose(data) , %must transpose data after reading in
fclose (fid) %close file
Matlab 还提供了许多其它的I/O 命令(函数) 来读写特定类型的文件,如读入图形文件IMREAD ,可支持读入. TIFF、. BMP、. JPEG、. HDF、. PCX、. XWD 等格式的图形文件。
WAVEREAD 可以读入声音文件,这里不再详述,可参阅相应帮助文档。