Using textscan with mixed data type in a single field/array
Afficher commentaires plus anciens
Hello,
I am having trouble reading a large (~30,000 rows) text file into Matlab. The data looks something like this:
BLOCK
1) 1996/01/01 00:00:00 -99.000N -99.000N
2) 1996/01/01 00:15:00 -99.000N -99.000N
3) 1996/01/01 00:30:00 -99.000N -99.000N
4) 1996/01/01 00:45:00 -99.000N -99.000N
5) 1996/01/01 01:00:00 -99.000N -99.000N
- skipped rows
16455) 1996/06/20 09:30:00 -99.000N -99.000N
16456) 1996/06/20 09:45:00 -99.000N -99.000N
16457) 1996/06/20 10:00:00 -99.000N -99.000N
16458) 1996/06/20 10:15:00 1.869T 0.088T
16459) 1996/06/20 10:30:00 1.892 0.083
16460) 1996/06/20 10:45:00 1.913 -0.082
16461) 1996/06/20 11:00:00 1.913 -0.064
16462) 1996/06/20 11:15:00 1.895 0.035
I use textscan to read in the data like this:
textFilename = [year,SID,'.txt'];
fid = fopen(textFilename, 'rt');
C = textscan(fid, '%*s%d/%d/%d%d%c%d%c%d%f%c%f%c','Headerlines',11);
The problem (as you can see from the data) is some of the values in the last two columns contains a letter alongside it. As this doesn't apply to all rows, when I consider this letter as a character (%c), where it doesn't appear, textscan moves along and reads the '-' symbol from the next integer. Thus, the values from the fourth column are incorrectly read as positive where they are actually negative.
My question is that how can I tell textscan to read in the values from the last two columns whilst somehow separating the letters...
Any and all help greatly appreciated!
Ozgun
4 commentaires
dpb
le 2 Juin 2014
Do you need the 'N' or 'T'?
If not, I've never tried it so don't know how it might work, but might try adding the two characters to the 'whitespace' definition.
If do need 'em, looks like job for regexp or line-by-line parsing, unfortunately.
Ozgun
le 2 Juin 2014
Cedric
le 2 Juin 2014
The first, important point to answer if the first question asked by dpb: do you need 'N' and 'T'.
Réponse acceptée
Plus de réponses (2)
Image Analyst
le 3 Juin 2014
If you can add a header row giving the names of the columns, then you can simply use the new table data type:
t = readtable('organz.txt')
Nice and simple.
Ozgun
le 3 Juin 2014
0 votes
Catégories
En savoir plus sur Text Data Preparation dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!