At every nth line read n lines together
7 次查看(过去 30 天)
显示 更早的评论
time,n0v_soma,n1v_soma,n2v_soma,n3v_dend
0,-70,-70,-66.5,-66.5
0.027,-69.9703,-69.9794,-66.4966,-66.4966
0.054,-69.9685,-69.9533,-66.4933,-66.4933
0.081,-69.9283,-69.9322,-66.49,-66.4899
0.108,-69.9588,-69.9385,-66.4866,-66.4866
0.135,-69.9422,-69.8965,-66.4833,-66.4833
0.162,-69.9058,-69.8943,-66.48,-66.48
0.189,-69.8465,-69.8618,-66.4767,-66.4766
0.216,-69.8505,-69.8258,-66.4734,-66.4733
0.243,-69.7855,-69.8011,-66.4701,-66.47
0.27,-69.7913,-69.7594,-66.4668,-66.4667
0.297,-69.7228,-69.7291,-66.4636,-66.4634
0.324,-69.6841,-69.7006,-66.4603,-66.4601
0.351,-69.6907,-69.7157,-66.4571,-66.4569
0.378,-69.6584,-69.7295,-66.4538,-66.4536
0.405,-69.5766,-69.7279,-66.4506,-66.4503
0.432,-69.5668,-69.6756,-66.4474,-66.447
0.459,-69.5396,-69.6777,-66.4442,-66.4437
0.486,-69.511,-69.6717,-66.441,-66.4404
0.513,-69.4808,-69.6523,-66.4378,-66.4371
0.54,-69.4632,-69.6325,-66.4346,-66.4339
0.567,-69.4605,-69.6169,-66.4314,-66.4306
0.594,-69.4398,-69.6346,-66.4282,-66.4273
0.621,-69.4638,-69.6245,-66.4251,-66.424
0.648,-69.4429,-69.5968,-66.4219,-66.4207
0.675,-69.4134,-69.5639,-66.4187,-66.4174
0.702,-69.3973,-69.5655,-66.4156,-66.4141
0.729,-69.3973,-69.5668,-66.4125,-66.4108
0.756,-69.3611,-69.6189,-66.4093,-66.4075
My file looks something like this. I want to jump to every 10th line in the file and take that line and following 2 lines and then jump to the 10th line and do so forth and so on E.g The output should be like this
0.216,-69.8505,-69.8258,-66.4734,-66.4733
0.243,-69.7855,-69.8011,-66.4701,-66.47
0.27,-69.7913,-69.7594,-66.4668,-66.4667
0.486,-69.511,-69.6717,-66.441,-66.4404
0.513,-69.4808,-69.6523,-66.4378,-66.4371
0.54,-69.4632,-69.6325,-66.4346,-66.4339
I don't want to read by looping
0 个评论
采纳的回答
Walter Roberson
2016-7-14
per_line = 5;
every_nth_line = 10;
read_in_a_row = 3;
fmt = [repmat('%*f', 1, per_line*(every_nth_line-read_in_a_row)), repmat('%f', 1, per_line*read_in_a_row)];
fid = fopen('YourFile.txt', 'rt');
datacell = textscan(fid, fmt, 'delimiter', ',\n', 'HeaderLines', read_in_a_row-1, 'CollectOutput', 1);
fclose(fid);
C = reshape(datacell{1}.', per_line, []).';
With the data you posted, the result would be
0.216 -69.8505 -69.8258 -66.4734 -66.4733
0.243 -69.7855 -69.8011 -66.4701 -66.47
0.27 -69.7913 -69.7594 -66.4668 -66.4667
0.486 -69.511 -69.6717 -66.441 -66.4404
0.513 -69.4808 -69.6523 -66.4378 -66.4371
0.54 -69.4632 -69.6325 -66.4346 -66.4339
0.756 -69.3611 -69.6189 -66.4093 -66.4075
NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN
The NaN are from the missing two lines after the 30th line that starts with 0.756, which you omitted from your output
更多回答(2 个)
Azzi Abdelmalek
2016-7-14
You can't do it without a for loop unless you read all the file, then select the lines you want
0 个评论
Shameer Parmar
2016-7-14
Hello Shivik,
Here is the Logic..
let us consider you have abc.txt file which is having these data.. So apply following logic and you will get the expected results.. Make sure that the abc.txt file is present in current directory of your working folder..
Data = textread('abc.txt', '%s', 'delimiter', '');
countNew = 1;
for count = 10:10:length(Data)
NewData(countNew,1) = Data(count);
if count+1 <= length(Data)
NewData(countNew+1,1) = Data(count+1);
end
if count+2 <= length(Data)
NewData(countNew+2,1) = Data(count+2);
end
countNew = countNew+3;
end
then type "NewData" and output will be :
NewData =
'0.216,-69.8505,-69.8258,-66.4734,-66.4733'
'0.243,-69.7855,-69.8011,-66.4701,-66.47'
'0.27,-69.7913,-69.7594,-66.4668,-66.4667'
'0.486,-69.511,-69.6717,-66.441,-66.4404'
'0.513,-69.4808,-69.6523,-66.4378,-66.4371'
'0.54,-69.4632,-69.6325,-66.4346,-66.4339'
'0.756,-69.3611,-69.6189,-66.4093,-66.4075'
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!