当前位置:首页 > matlab文件数据读写
% 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_file2.txt','%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_file2.txt',... '%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_file2.txt','%*s %d %d %f','headerlines',3) DLMREAD/DLMWRITE/CSVREAD
DLMREAD 和 DLMWRITE函数能够读写分隔的ASCII data,而不是用low level routines。他们比low level routines容易使用,Low level routines用几行代码实现的功能可以用DLMREAD/DLMWRITE简化成一行。
CSVREAD用来读分隔符是逗号的文件,是DLMREAD的特殊情况。当读空格和Tab分隔的电子数据表文件时,DLMREAD特别有用。以'sample_file.txt'为例:
CODE:
Example 1: Using DLMREAD to read in a file with headers, text, and numeric data
% This reads in the file 'sample_file2.txt' and creates a matrix, D, % with the numeric data this command specifies a white space as the % delimiter of the file
D = dlmread('sample_file.txt','')
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_file.txt'into the matrix, D_partial.
% 读文件 'sample_file.txt' 前3列后3行,到矩阵D_partial.
D_partial = dlmread('sample_file.txt','',[2 0 4 2])
CODE:
Example 3: Using DLMWRITE to write a comma delimited file
% This creates a file called 'partialD.txt' that consists of % the first 3 columns of the last 3 rows of data where each % element is separated by a comma
dlmwrite('partialD.txt',D_partial,',')
注意: 保证DLMREAD and DLMWRITE指定范围的指标从0开始,而不是从1开始。
WK1READ/WK1WRITE
WK1READ 用来读Lotus123 电子数据表文件的数据;WK1WRITE用来写矩阵到Lotus123 电子数据表文件。
XLSREAD
XLSREAD用来读Excel的数值和文本数据。
三. 具体例子分析:
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 5 3728.769 2212.433 141.277 6 3738.785 2214.381 141.256 7 3728.759 2214.261 141.228 8 3748.886 2214.299 141.243 9 3748.935 2212.417 141.253 10 3733.612 2226.653 141.236 11 3733.583 2229.248 141.223 12 3729.229 2229.118 141.186
解答:对于这个txt文件,由于各行列数相同,故简单地使用load,importdata均可。
2.字段名(中、英文字段均可)+数据: 源文件:
CODE:
CH0 CH1 CH2 CH3
0.000123 0.000325 0.000378 0.000598 0.000986 0.000256 0.000245 0.000698
解答:由于是记录的形式,因此各行列数必相同(缺少部分列时请自行在文件中补上 Inf 或 NaN),故直接使用 importdata 便可。
3.注释(含有独立的数字串)+数据(列数相同):
问题:这个文件有4列,但前6行是文字说明,4列数字是从第8行开始的.现在我想把这个文件的前2列和文字说明提出来组成一个新的dat文件
源文件:
CODE:
Group 2 12.02.2006 Limei Samples of datas: 50000
CH0 CH1 CH2 CH3
0.000123 0.000325 0.000378 0.000598
0.000986 0.000256 0.000245 0.000698
目标文件:
CODE:
Group 2 12.02.2006 Limei Samples of datas: 50000
CH0 CH1
0.000123 0.000325 0.000986 0.000256
解答: 由于注释中含有独立的数字串,且注释部分没有明显的格式, 这时候用importdata, load等高级命令直接读取会失败,用 textread, dlmwrite 等格式化命令也不太合适,因此只能使用低级命令进行读取。(当然了,可以跳过注释部分直接用高级命令读取数据,即:[a b c d] = textread(filename,'%f %f %f %f','headerlines',4); )。一个简单的、非通用的包含注释的读取方法如下:
-------------------------------------转
---------------------------------------------------------------------------------------
CODE: clc;clear;
fid = fopen('exp.txt', 'r'); fid_n=fopen('ex.dat','w'); while ~feof(fid) tline=fgetl(fid); if ~isempty(tline)
if double(tline(1))>=48 && double(tline(1))<=57 %数值开始 a=strread(tline); a(3:4)=[];
fprintf(fid_n,'%f %f\\n',a); clear a;
elseif double(tline(1))==67 %字母C开始 [b1,b2,b3,b4]=strread(tline,'%s %s %s %s'); b=[b1{1},' ',b2{1}]; fprintf(fid_n,'%s\\n',b); clear b b1 b2 b3 b4; else
fprintf(fid_n,'%s\\n',tline); end else
共分享92篇相关文档