reading a specific table out of txt file

조회 수: 5 (최근 30일)
Baruch
Baruch 2013년 7월 27일
hello,
i have a text file which contains both strings and numbers and a table of numbers at the end of it, the table consists of 21 columns, the number of rows can change from file to file.
a string row of " T N r5 r10 ..." and so forth marks the labels of the numeric table.
i would like to read ONLY the table on a column base and ignore the rest of the file, keeping the string headline could be useful but not very important.
first 25 lines (rows) are irrelevant there is more then one file but they all follow the same format.it generally looks like this:
(the web page format distorts the spacing, i have add a " // " in place were there's a line break, it does NOT exist in the original file
GITCO_npp_d20120719_t1922439_e1928243_b03766_c20120720192653097790_noaa_ops.h5 href = ""</a> region available pixel number = 1212 // region lowest temperature = -20.9843 // 36.6178 36.4885 36.2677 36.4210 // -97.2899 -97.0011 -97.2073 -97.4923 // solar transmitance at moist atmosphere at 20C = 0.600000 // min transmittance factor of solar 3.7 radiance = 0.600000 // temperature above which the pixel is most likely ground 23.0000 // ct4-ct5 brightness temp diff, for thin clouds,used in window = 1.00000 // ct4-ct5 brightness temp diff, for thin clouds,used for T-Re = 1.00000 // ct4-ct5 brightness temp diff at cloud base = 5.00000 // T5 Correction = T5 + 0.0000000E+00 * T5 + 0.0000000E+00 // Selecton of pixels with temperature gradient smaller than 999.000 // vis reflectance below which the pixel is most likely ground 0.400000 // vis reflectance below which the pixel is most likely ground for T-Re region 0.500000 // IFILTER = 0 // Classification percentile = 30 // IAPPLY_TMIN = 0 // CORR_THRESH = 1.00000 // Zone Tstart Tend // 1 13 -12 // 2 -13 -21 // 3 99 99 // 4 99 99 // 5 99 99 T N r5 r10 r15 r20 r25 r30 r35 r40 r45 r50 r55 r60 r65 r70 r75 r80 r85 r90 r95 r100 // -21 1 14.1 14.1 14.1 14.1 14.1 14.1 14.1 14.1 14.1 14.1 14.1 14.1 14.1 14.1 14.1 14.1 14.1 14.1 14.1 14.1 // -20 3 12.3 12.3 12.3 12.3 12.3 12.3 12.3 12.3 12.3 12.3 12.3 12.3 12.3 12.3 12.3 12.3 12.3 12.3 12.3 12.3 // -19 4 11.7 11.7 11.7 11.7 11.7 11.7 11.7 12.0 12.0 12.0 12.0 12.0 13.6 13.6 13.6 13.6 13.6 14.1 14.1 14.1 // -18 3 11.8 11.8 11.8 11.8 11.8 11.8 11.8 11.8 11.8 12.1 12.1 12.1 12.1 12.1 12.1 12.1 13.8 13.8 13.8 13.8 // -17 6 10.8 10.8 10.8 10.8 11.0 11.0 11.0 11.0 11.5 11.5 11.5 11.7 11.7 11.7 12.2 12.2 12.2 12.2 14.2 14.2 // -16 4 11.1 11.1 11.1 11.1 11.1 11.1 11.1 11.5 11.5 11.5 11.5 11.5 12.0 12.0 12.0 12.0 12.0 16.0 16.0 16.0 // -15 6 10.7 10.7 10.7 10.7 10.8 10.8 10.8 10.8 11.7 11.7 11.7 11.9 11.9 11.9 13.6 13.6 13.6 13.6 14.8 14.8 // -14 3 10.7 10.7 10.7 10.7 10.7 10.7 10.7 10.7 10.7 11.2 11.2 11.2 11.2 11.2 11.2 11.2 15.0 15.0 15.0 15.0 // -13 19 10.4 10.8 10.8 10.9 11.1 11.2 11.3 11.3 11.5 11.5 11.5 11.6 11.8 12.0 12.9 13.3 13.6 13.9 16.3 16.4 // -12 9 11.1 11.1 11.1 11.2 11.2 11.5 11.5 11.6 11.6 11.6 11.6 11.6 11.7 11.7 11.7 11.7 13.1 13.1 14.7 14.7 // -11 13 10.4 10.4 10.4 10.4 10.4 10.6 10.9 10.9 10.9 10.9 10.9 11.0 11.0 12.0 12.3 12.3 12.5 12.5 12.5 14.1 // -10 13 10.0 10.0 10.0 10.0 10.0 10.5 10.6 10.6 10.8 10.8 10.8 11.6 11.6 11.7 11.8 11.8 12.3 13.6 13.6 13.7 // -9 41 10.3 10.5 10.6 10.8 10.9 10.9 11.0 11.2 11.3 11.3 11.5 11.6 11.8 12.0 12.0 12.3 12.6 13.1 13.2 14.0 // -8 54 10.0 10.1 10.3 10.5 10.6 10.7 10.8 11.0 11.1 11.2 11.3 11.4 11.5 11.7 11.8 12.0 12.2 12.7 13.1 15.6 // -7 38 9.3 9.8 10.0 10.3 10.5 10.5 10.6 10.7 10.8 10.8 10.9 10.9 11.1 11.5 11.6 11.7 11.8 11.9 12.5 13.6 href = ""</a> -6 67 8.8 9.1 9.6 9.7 9.9 10.0 10.1 10.2 10.3 10.4 10.5 10.6 10.7 10.8 10.8 10.9 11.1 11.7 12.1 13.6 href = ""</a> -5 67 8.9 9.2 9.5 9.6 9.7 9.8 9.9 10.1 10.1 10.2 10.3 10.3 10.4 10.6 10.6 10.8 10.9 11.2 11.6 12.6 href = ""</a> -4 69 8.7 9.0 9.1 9.3 9.4 9.5 9.6 9.7 9.8 9.9 10.1 10.2 10.4 10.5 10.8 10.9 11.3 11.6 11.9 12.3 href = ""</a> -3 61 7.9 8.2 8.4 8.6 8.8 9.0 9.1 9.1 9.3 9.5 9.7 9.8 9.9 10.1 10.2 10.5 10.9 11.2 12.1 13.1 href = ""</a> -2 62 7.3 7.5 7.8 8.0 8.3 8.7 8.9 9.0 9.1 9.2 9.5 9.8 9.9 10.2 10.4 10.7 10.8 11.3 11.9 13.0 href = ""</a> -1 78 7.1 7.3 7.6 7.9 8.2 8.2 8.4 8.6 8.9 9.1 9.2 9.4 9.5 9.5 9.6 10.0 10.5 11.2 11.5 12.1 href = ""</a> 0 75 6.9 7.6 7.7 8.1 8.3 8.5 8.7 8.8 8.9 9.2 9.3 9.4 9.8 10.1 10.3 10.7 10.9 11.6 11.7 13.1 href = ""</a> 1 62 6.9 7.3 7.5 8.0 8.1 8.4 8.4 8.6 8.6 8.8 9.0 9.2 9.4 9.5 10.0 10.3 10.7 11.4 11.7 13.0 href = ""</a> 2 64 6.2 7.0 7.6 8.0 8.1 8.3 8.5 8.5 8.6 8.7 8.9 9.0 9.2 9.5 9.7 9.8 10.2 11.7 12.1 12.9 href = ""</a> 3 47 6.2 7.2 7.6 7.7 7.9 8.0 8.1 8.1 8.2 8.5 8.5 8.7 8.9 9.3 9.5 9.7 9.8 10.0 11.1 15.2 href = ""</a> 4 77 6.3 6.9 7.2 7.3 7.5 7.6 7.8 7.9 8.1 8.2 8.3 8.4 8.8 9.0 9.1 9.6 9.8 9.9 11.4 12.5 href = ""</a> 5 55 5.7 6.0 6.1 6.7 6.8 7.0 7.1 7.3 7.4 7.5 7.6 7.8 7.9 8.3 8.4 8.5 9.0 9.4 9.7 11.3 href = ""</a> 6 66 5.4 5.9 6.1 6.5 6.8 6.9 7.1 7.1 7.3 7.5 7.5 7.9 8.0 8.1 8.6 8.8 8.8 9.2 10.0 10.8 href = ""</a> 7 40 5.4 6.3 6.4 6.6 6.7 6.7 6.9 7.1 7.3 7.5 7.6 7.9 8.1 8.2 8.3 8.6 8.8 8.9 9.3 9.6 8 33 4.8 5.0 5.6 5.9 6.0 6.3 6.4 6.4 6.6 6.8 6.8 6.9 7.0 7.1 7.4 8.0 8.4 8.5 8.6 8.8 href = ""</a> 9 20 3.8 5.2 5.6 5.8 5.9 5.9 6.0 6.2 6.4 6.4 6.5 6.5 6.7 7.0 7.2 7.3 7.4 8.5 9.0 9.2 href = ""</a> 10 15 4.0 4.1 4.1 4.1 4.2 5.0 5.0 5.3 6.2 6.2 6.2 6.3 6.9 7.2 7.2 7.2 7.5 7.7 7.7 7.8 href = ""</a> 11 22 2.4 3.3 3.3 3.6 4.0 4.1 4.2 4.5 4.6 4.7 5.3 5.4 5.4 5.5 5.9 6.1 6.6 6.8 6.9 8.5 12 5 2.4 2.4 2.4 2.4 2.4 4.5 4.5 4.5 4.5 5.2 5.2 5.2 5.2 5.7 5.7 5.7 5.7 7.4 7.4 7.4 href = ""</a> 13 10 2.9 2.9 3.3 3.3 4.0 4.0 4.1 4.1 4.1 4.1 4.2 4.2 4.6 4.6 4.7 4.7 4.9 4.9 5.2 5.2 href = ""</a>
the line of " T N r5 r10 ..." and so forth marks the labels of the numeric table.
i would like to read ONLY the table on a column base and ignore the rest of the file, keeping the string headline could be useful but not very important. there are 21 columns.
  댓글 수: 1
dpb
dpb 2013년 7월 27일
Use the {}Code button (or insert two leading spaces on first line of the text file to format it w/o wordwrap. (_WHY_ of why TMW can't figure out that for a code forum wordwrap OFF by default should be the norm and only if user selects it should WYSWIG be on is beyond my ken :( ).
Anyway, shorten the sample file to only a few pertinent lines are reformat...it's not legible as is w/ too much effort.

댓글을 달려면 로그인하십시오.

답변 (1개)

dpb
dpb 2013년 7월 27일
편집: dpb 2013년 7월 27일
If the 25 header lines is consistent, it's trivial; otherwise you'll have to have something that is consistent-enough to search for.
I'll assume from what you've said the lines are consistent; it appears the variable header line is the 25th by my count/reformatting above...
fid=fopen('yourfile.txt','r');
hdr=textscan(fid,'%s\n','headerlines',24); % gets first field in header row
hdr=[char(hdr{:}) ' ' fgetl(fid)] % scan the rest; append
n=length(strfind(strtrim(hdr),' '))+1; % find out how many columns
d=fscanf(fid,'%f',[n inf])'; % and get the data...
fid=fclose(fid);

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by