[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: read file, given a non-octave header
From: |
Thomas Treichl |
Subject: |
Re: read file, given a non-octave header |
Date: |
Sat, 27 Jan 2007 14:21:47 +0100 |
User-agent: |
Thunderbird 1.5.0.9 (Macintosh/20061207) |
the_verge schrieb:
Ok, One more data loading quesiton:
I am loading a file that has some comment lines preceded by the '#'
character, and then two data description lines, and then the data. The
first description line is a tab separated list of the variables in the file:
agency_cd site_no parameter_cd dd_nu year_nu month_nu mean_va
The second line is a list of the number of characters ("s") or numbers ("n")
each column takes up:
5s 15s 5s 3n 4s 2s 12n
Following that, the data is in columns corresponding to the previous two
description lines.
What I want to do is load the data into appropriately named vectors, (some
of the columns are strings, some are numbers) just using the information
from the first two "description lines." I am doing this a vast number of
times, so automation is necessary (in other words, I can't edit each file by
hand to put it into a certain format).
How do I parse this out?
Hi Vergil,
please next time sent a short (about 5 lines long) example data file, so that
nobody needs to create a data file first. My data file is (and I hope yours
looks something like that)
# comment line 1 can be ignored
# comment line 2 can be ignored
agency_cd site_no parameter_cd dd_nu year_nu month_nu mean_va
5s 15s 5s 3n 4s 2s 12n
hallo abcde67890abcde world 123 abcd ab 123456789012
world 12345abcde12345 hallo 456 efgh cd 210987654321
And the function you would use could look something like
function vret = parsedata (filename)
[vfid, vmsg] = fopen (filename, "r");
if (isempty (vmsg) == false), error (vmsg); endif
fgetl (vfid); fgetl (vfid); # Forget about the first two lines
vcnt = 1; # Initialize counter for while loop
vstr = fgetl (vfid); # Get third line
while (vstr != -1)
[vret{vcnt,1}, vret{vcnt,2}, vret{vcnt,3}, vret{vcnt,4}, \
vret{vcnt,5}, vret{vcnt,6}, vret{vcnt,7}] = \
sscanf (vstr, "%s\t%s\t%s\t%s\t%s\t%s\t%s", "C");
vstr = fgetl (vfid);
vcnt = vcnt + 1;
endwhile
fclose (vfid);
endfunction
Your return value is of type "cell array of strings" resp. a matrix of cells, so
if you need to convert your number strings into real numbers you need to change
the above code or write some more.
Thomas