help-octave
[Top][All Lists]
Advanced

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

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


From: Francesco Potortì
Subject: Re: Import large field-delimited file with strings and numbers
Date: Sun, 07 Sep 2014 22:59:33 +0200

I rearranged to follow this list's convention of bottom posting.  Please
answer at the end.

>On 07 Sep 2014, at 22:17, Francesco Potortì <address@hidden> wrote:
>
>>> 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...
>> 
>> 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); [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 :(

Petri Piila;
>I succeed in extracting the last two numeric columns by applying %*s to the 
>first two string columns:
>
>octave:76> f = fopen("test.txt","r")
>f =  4
>octave:77> fgetl(f)
>ans = field1, field2, field3, field4
>octave:78> c34 = fscanf(f,"%*s%*s%d,%f,",[2,Inf]);
>octave:79> c34'
>ans =
>
>   1   1
>   2   2
>   3   3
>
>octave:80> fclose(f);
>
>However, I’m not able to extract the strings accordingly.

You need the "C" approach to to that.

>With the “C” approach you used, I think you have to loop.

While the docs are not clear on this, it seems not.  Since fscanf
returns vectors, I suppose it is intended to read until end of file.
But I cannot seem to have it work...

-- 
Francesco Potortì (ricercatore)        Voice:  +39.050.621.3058
ISTI - Area della ricerca CNR          Mobile: +39.348.8283.107
via G. Moruzzi 1, I-56124 Pisa         Skype:  wnlabisti
(entrance 20, 1st floor, room C71)     Web:    http://fly.isti.cnr.it



reply via email to

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