Reading a .txt file

3 views (last 30 days)
Pooja
Pooja on 10 Sep 2014
Commented: dpb on 11 Sep 2014
I am trying to read a .txt file with different no. of column in each row. Also first few lines contain header information. First few lines for the files are pasted below
Version 8.0 n19 SBUV data for day 006 2011 (2011/01/06)
1288 :Number of records
1) year day sec-gmt Lat Lon SZA Total_Ozone Reflectivity Aerosol_Index Quality_residue Error_Flag
2) ozone (DU) in 13 layers -- pressure level at the bottom of each layer(atm):
1.000 0.0631 0.0400 0.0251 0.0158 0.0100 0.0063 0.0040 0.00251 0.00158 0.0010 0.00063 0.00040
3) ozone (PPMV) at 15 pressure levels(hPa):
0.5 0.7 1.0 1.5 2.0 3.0 4.0 5.0 7.0 10.0 15.0 20.0 30.0 40.0 50.0
2011 6 5235 -71.26 -25.20 86.12 276.3 0.755 -0.8 0.032 11
99.76 46.51 41.39 29.819 22.048 15.650 10.123 5.814 2.846 1.286 0.5925 0.2722 0.2207
1.400 1.773 2.253 2.994 3.711 4.753 5.222 5.389 5.371 5.059 4.439 4.008 3.617 3.184 2.593
2011 6 5267 -72.87 -28.35 84.42 279.8 0.797 -1.4 0.031 11
101.67 47.43 41.91 29.989 22.153 15.722 10.123 5.744 2.816 1.271 0.5663 0.2516 0.2030
1.290 1.669 2.190 2.975 3.675 4.685 5.186 5.388 5.394 5.084 4.460 4.029 3.657 3.239 2.645
I am interested in reading data starting from 2011. I used fscanf and dlmread. fscanf could not work exactly. Also dlmread read few of the columns of the data. But not the full data. Kindly help me to read the data from this txt file. dlmread was used as M=dlmread(filename,'\t',8,0);
Thanks in advance

Accepted Answer

dpb
dpb on 10 Sep 2014
Edited: dpb on 10 Sep 2014
Of course fscanf can read it if you work on the formatting sufficiently. With an irregular file structure, though, it'll be prudent to use the facilities of textscan to ease the burden somewhat if trying to read the file on the fly.
I'll take a slightly different tack here, though, since the data you're interested in are regularly spaced and just differ by the number of elements/row.
dlmread can only work on numeric-only data, per the doc--
>> help dlmread
dlmread Read ASCII delimited file.
RESULT = dlmread(FILENAME) reads numeric data from the ASCII
delimited file FILENAME. The delimiter is inferred from the formatting
of the file.
...
All data in the input file must be numeric. dlmread does not operate
on files containing nonnumeric data, even if the specified rows and
columns for the read contain numeric data only.
I pasted your sample data into a file...therein the linewrap shown in the forum disappeared so I presume that's simply a fignewton of the display.
Then, just read the whole file into memory as an array of cell strings and operate from memory to convert...I used textread as it's so simple and useful despite TMW having relegated it to the status or red-haired stepchild--use textscan or whatever instead if you prefer--
After that, just convert the subsequent lines to character strings selecting each group and let str2num do the hard work for you--
>> f=textread('pooj.dat', '%s', 'delimiter', '\n', ...
'whitespace', '','headerlines',8);
>> hdr=str2num(char(f(1:3:end)))
hdr =
1.0e+03 *
Columns 1 through 9
2.0110 0.0060 5.2350 -0.0713 -0.0252 0.0861 0.2763 0.0008 -0.0008
2.0110 0.0060 5.2670 -0.0729 -0.0284 0.0844 0.2798 0.0008 -0.0014
Columns 10 through 11
0.0000 0.0110
0.0000 0.0110
>> DU=str2num(char(f(2:3:end)))
DU =
Columns 1 through 9
99.7600 46.5100 41.3900 29.8190 22.0480 15.6500 10.1230 5.8140 2.8460
101.6700 47.4300 41.9100 29.9890 22.1530 15.7220 10.1230 5.7440 2.8160
Columns 10 through 13
1.2860 0.5925 0.2722 0.2207
1.2710 0.5663 0.2516 0.2030
>> PPM=str2num(char(f(3:3:end)))
PPM =
Columns 1 through 9
1.4000 1.7730 2.2530 2.9940 3.7110 4.7530 5.2220 5.3890 5.3710
1.2900 1.6690 2.1900 2.9750 3.6750 4.6850 5.1860 5.3880 5.3940
Columns 10 through 15
5.0590 4.4390 4.0080 3.6170 3.1840 2.5930
5.0840 4.4600 4.0290 3.6570 3.2390 2.6450
>>
Done! :)
  2 Comments
Pooja
Pooja on 11 Sep 2014
Can u pls let me know in
DU=str2num(char(f(2:3:end)))
what is the meaning using 3 here?
dpb
dpb on 11 Sep 2014
Read
doc colon % maybe???
Or, try
(2:3:10)
at the command line and see a numeric example.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!