Why is sscanf skipping 2 columns on second line?

1 次查看(过去 30 天)
Hi!
I have a text file that has four numeric values on every other line and a date on every other. Sscanf auotmatically skips the date lines, just as I want it to. Howver, when it starts reading the numeric lines, on the second line it only grabs values from the first two columns(four exist altogether). All the other lines are treated correctly. What might be going on? The source file in question is attached to this message.
THanks a lot in advance!
fid2=fopen('allMeasurements.txt','at');
R=0;
D= [9 11 12];
for i = D;
s1=num2str(i);%Next lines creat filename
if i<10;
s0='0';
s1=strcat(s0,s1);
end
s0='000';
s=strcat(s0,s1,'.txt');
sd='data';
s=strcat(sd,s);
fid=fopen(s, 'rt');
while fgetl(fid)~=-1;
R=R+1;
A = sscanf(tline,'%f', [1,4]);%The 4 columns are stored in A
fprintf(fid2,'%f %f\n',B);
end
fclose(fid);
end
fclose(fid2);
  3 个评论
Mak
Mak 2017-5-31
Hi Guillaume and thanks for the tips on naming. I should have left the last line, before "end", out. But the line that starts: A =... That line produces a 1x4 matrix on each iteration, EXCEPT when it's on the second numeric line of the file(which I have attached). What I want to know is why it treats the second numeric line different to all the rest? They all seem the same.
per isakson
per isakson 2017-5-31
编辑:per isakson 2017-5-31
Remove the extra dot on line 4
and try
fid = fopen('data00009.txt');
cac = textscan( fid, '%*s\n%f%f%f%f', 'Delimiter','\t', 'CollectOutput',true );
fclose( fid );
>> cac
cac =
[60x4 double]
>> version
ans =
9.0.0.341360 (R2016a)

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Text Data Preparation 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by