请教MATLAB去读文本文件中的数据

来源:百度知道 编辑:UC知道 时间:2024/06/18 13:04:27
需要从文本文件中读取数据,我的文件很大,格式如下:
********** output: slice 35
=================
5.9989E+02 current

power increment p_mid phi_mid r_size angle energy bunching xrms yrms error <x> <y> e-spread far_field
1.0000E+00 4.4409E-14 2.5253E-02 -5.8757E-32 4.2314E-05 3.7847E-05 -2.0000E+00 1.3632E-04 1.1492E-04 7.0876E-05 0.0000E+00 3.6676E-09 -6.2433E-10 1.6261E-01 8.9205E+11
1.0000E+00 -8.8818E-15 2.5736E-02 -1.9881E-02 4.1910E-05 3.7847E-05 -2.0000E+00 1.3438E-04 1.1529E-04 7.0641E-05 0.0000E+00 3.6854E-09 -6.2217E-10 1.6261E-01 8.9205E+11
1.0000E+00 0.0000E+00 2.6211E-02 -4.0115E-02 4.1522E-05 3.7847E-05 -2.0000E+00 1.3259E-04 1.1565E-04 7.0408E-05 0.0000E+00 3.7033E-09 -6.2001E-10 1.6261E-01 8.9205E+

传文件的前十行左右到我的邮箱吧。
wacs5@126.com

===========代码如下========

clear
fid=fopen('d:\data.txt','r'); %路径你得调一下
cntline=0; %数据行数
cntseg=0; %段数
indseg=[1]; %每一段在总体数组中的位置
while (~feof(fid))
line=fgetl(fid);
len=length(line);
if (len) %非空行
i=1;
while(line(i)==' ') %去掉行头无用的信息
i=i+1;
end

if (line(i)<'0' || line(i)>'9') %如果不是数字,即字符
if (line(i)=='*') %如果出现*号,说明新的一段开始
cntseg=cntseg+1;
indseg(cntseg)=cntline+1; %新段的索引号
end
continue;
else %如果是数据行的话
if isempty(strfind(line(i:end),'current')) %不含current的数据行
cntline=cntline+1;
data(cntline,:)=sscanf(line(i:end),'%f')';
else %含有current
currentdata(cntseg,1)=sscanf(line(i:end),'%f');
end
end
end
end
fclose(fid);

load

不懂