Reading in only the first several lines of a txt file with headers and unknown number of rows .
20 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
0 commentaires
Réponses (1)
Star Strider
le 6 Mai 2017
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 commentaires
Star Strider
le 6 Mai 2017
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.
Voir également
Catégories
En savoir plus sur Low-Level File I/O dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!