Using textscan with mixed data type in a single field/array
이전 댓글 표시
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
dpb
2014년 6월 2일
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
2014년 6월 2일
Cedric
2014년 6월 2일
The first, important point to answer if the first question asked by dpb: do you need 'N' and 'T'.
채택된 답변
추가 답변 (2개)
Image Analyst
2014년 6월 3일
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.
카테고리
도움말 센터 및 File Exchange에서 Text Data Preparation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!