[Top][All Lists]

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

Re: Import large field-delimited file with strings and numbers

From: Philip Nienhuis
Subject: Re: Import large field-delimited file with strings and numbers
Date: Mon, 8 Sep 2014 01:49:48 -0700 (PDT)

Francesco Potortì wrote
> Joao Rodrigues:
>>>> To answer Francesco's comment, fscanf allows for size specification 
>>>> without loop, but I don't know how to write the template indicating
>>>> that 
>>>> commas (or something else) is a separator. Furthermore, even if the 
>>>> separator is whitespace as output I got a concatenated string instead
>>>> of 
>>>> multiple fields...
> Francesco Potortì:
>>> Hm.  I tried myself but can't get fscanf to read multiple lines...
>>> Here is what I get:
>>> ===File ~/math/workarea/test.txt============================
>>> field1, field2, field3, field4
>>> A,        a,        1,       1.0,
>>> B,        b,        2,        2.0,
>>> C,        c,        3,        3.0,
>>> ============================================================
>>> octave> fid=fopen("test.txt");
>>> octave> fgetl(fid);
>>> octave> [v1, v2, v3, v4, count, errmsg]=fscanf(fid, "%s%s%d,%f,", "C")
>>> v1 = A,
>>> v2 = a,
>>> v3 =  1
>>> v4 =  1
>>> count =  4
>>> errmsg = 
>>> octave> fclose(fid);
>>> So everything is nice and good, but fscanf stops at the first row of
>>> data...
>>> Oh, and I also tried twith a template "%s%s%d,%f,\n", explicitely
>>> adding a newline at the end, because the Octave manual does not say what
>>> a "whitespace" is, but no change :(
> Ben Abbott:
>>The doc-string indicates that a 2nd input is needed for fgetl() to read
more than one line.
> Hm, maybe the way I wrote it was cconfusing.  I now edited the above
> cited text by adding a newline and a prompt in my code for increased
> clarity.
> In the code, fgetl is just used to get rid of the header line.  My
> problem is with fscanf, which in my plan should read all the subsequent
> lines, but only reads the first one after the header line.

Hopefully I properly understand what you want to achieve, but to read the
rest of an entire file starting at some position, I usually do:

<....fseek or previous read operations....>
str = fread (fid, "char=>char").';  ## Note transpose operator


View this message in context:
Sent from the Octave - General mailing list archive at

reply via email to

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