Reading in only the first several lines of a txt file with headers and unknown number of rows .

23 次查看(过去 30 天)
I am trying to read in a space-delineated file that looks something like this:
# 21: blah balh blah
# 22: asdfsaf
# 23: agfgae
0 0.0e+00 64 659 289 0 7 -2.1e+04 2.18e+04 Ra_3.6460721 1000 Ra_3.le-00000
1 3.5e+09 64 659 289 21 14 -2.2e+04 2.21e+04 Ra_3.6460721 1000 Ra_3.le-00001
2 4.3e+09 64 659 289 13 14 -2.2e+04 2.29e+04 Ra_3.6460721 1000 Ra_3.le-00002
3 4.7e+09 64 659 289 10 14 -2.4e+04 2.42e+04 Ra_3.6460721 1000 Ra_3.le-00003
4 5.0e+09 64 659 289 9 14 -2.5e+04 2.59e+04 Ra_3.6460721 1000 Ra_3.le-00004
5 5.2e+09 64 659 289 9 14 -2.8e+04 2.82e+04 Ra_3.6460721 1000 Ra_3.le-00005
I am trying to skip the headers, and read the rest into a matrix. I've been getting errors with dlmread and load that seem to relate to the strings in the last few columns. With txetscan I was able to load one line at a time, but I can't seem to make it load all lines after the headers. I also wont know how many columns are in these files.
Some other notes: the columns I care about are 1 - 9 in this example. The number of header lines and columns is always the same in each file.
Any ideas on how I can do this?
Thanks

回答(1 个)

Star Strider
Star Strider 2017-5-6
We do not have your actual file, so there may be 'EndOfLine' problems you will have to deal with, and you will have to add your 'HeaderLines' value as well.
Those problems aside, this will get you started:
fidi = fopen('My_File.txt','rt');
D = textscan(fidi, [repmat('%f',1,9) '%*s%*s'], 'Delimiter',' ', 'MultipleDelimsAsOne',1, 'CollectOutput',1);
  2 个评论
modelhelp
modelhelp 2017-5-6
编辑:Walter Roberson 2017-5-6
Here is a text file. I tried running this and it only loads in 1 line, in this case row 4.
fidi = fopen('test.txt','rt');
D = textscan(fidi, [repmat('%f',1,9) '%*s%*s'],'HeaderLines',3,'Delimiter',' ', 'MultipleDelimsAsOne',2, 'CollectOutput',1);
Star Strider
Star Strider 2017-5-6
I can’t get textscan to read it correctly, even with several calls to it without the 'HeaderLines' argument (after reading the first line) using fseek to re-initialise the file pointer (a technique with other files), and several experiments with the 'EndOfLine' and other arguments, all of which failed. Even readtable has problems with it.
You can do something like this:
fidi = fopen('modelhelp test.txt','rt');
k1 = 0;
while k1 <= 9 % ~feof(fidi)
k1 = k1 + 1;
D{k1} = fgetl(fidi);
end
fclose(fidi);
You then have to parse each line manually.
I will delete my Answer in a few minutes, since it may not be possible to import your file.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Low-Level File I/O 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by