help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: read file, given a non-octave header


From: the_verge
Subject: Re: read file, given a non-octave header
Date: Sat, 27 Jan 2007 09:29:30 -0800 (PST)

Thomas,

Thanks for addressing my questions.  I'm really close to making it read
properly, but I'm having a small problem:  The first row in my cell array is
always the 3rd line of the file, regardless of whether it is a comment line
or a line of data.  For some reason Octave doesn't seem to be ignoring the
comments.  The reason it begins reading on the 3rd line is because of the
fgetl(fid); fgetl(fid); that you put in there to ignore the descriptor
lines.  Preferably, it would skip all 35 or so lines of comments, and then
skip the two data description lines, and then begin reading from the first
line of data.

Any ideas on how to make it skip comment lines (preceded by a #)?

Thanks,
Vergil

P.S. For reference, when I called the function that you wrote for me, here
was my resulting cell array (you can see the description lines in there):

data_cell_array =

{
  [1,1] = agency_cd
  [2,1] = 5s
  [3,1] = hallo
  [4,1] = world
  [1,2] = site_no
  [2,2] = 15s
  [3,2] = abcde67890abcde
  [4,2] = 12345abcde12345
  [1,3] = parameter_cd
  [2,3] = 5s
  [3,3] = world
  [4,3] = hallo
  [1,4] = dd_nu
  [2,4] = 3n
  [3,4] = 123
  [4,4] = 456
  [1,5] = year_nu
  [2,5] = 4s
  [3,5] = abcd
  [4,5] = efgh
  [1,6] = month_nu
  [2,6] = 2s
  [3,6] = ab
  [4,6] = cd
  [1,7] = mean_va
  [2,7] = 12n
  [3,7] = 123456789012
  [4,7] = 210987654321
}




Thomas Treichl wrote:
> 
> 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
> _______________________________________________
> Help-octave mailing list
> address@hidden
> https://www.cae.wisc.edu/mailman/listinfo/help-octave
> 
> 

-- 
View this message in context: 
http://www.nabble.com/read-file%2C-given-a-non-octave-header-tf3126856.html#a8668038
Sent from the Octave - General mailing list archive at Nabble.com.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]