|
From: | Thomas D. Dean |
Subject: | Re: how does fscanf with "C" work? |
Date: | Mon, 08 Sep 2014 10:11:28 -0700 |
User-agent: | Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.0 |
On 09/08/14 02:56, Francesco Potortì wrote:
I'll try to be more specific. If I get no answer from this list, I'll submit a bug report. I create a text file made of four lines, like this: ===File ~/math/workarea/test.txt============================ field1, field2, field3, field4 A, a, 1, 1.0, B, b, 2, 2.0, C, c, 3, 3.0, ============================================================ Then this is what i get: octave> version ans = 3.8.2 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);
I have not been following this, but, fscanf is doing exactly what you asked it to.
Read 'help fscanf' carefully. You are using the second form. fd=fopen("xx.dat") fgetl(fd) [v1, v2, v3, v4, count, errmsg] = fscanf(fd, "%s%s%d,%f", "C") This is iterative and so, slow.You can read the entire file using the first form of fscanf. However, since the strings are not quoted, octave will translate the ascii characters to numerical byte values.
A==65, B==66, etc. fd=fopen("xx.dat") fgetl(fd) [A,count,msg]=fscanf(fd,"%s%s%d,%f%s")The contents of A show this conversion and also, the extra ',' on each line that must be accounted for in the format.
If you only need the numerical values, you can use csvread()If you do that, you will notice there are 5 columns, not 4. And, the strings are converted to zeros.
A= csvread("xx.dat")By the way, your first paragraph sounds threatening and makes many people not answer. And, a bug report on this will most likely be ignored.
Tom Dean
[Prev in Thread] | Current Thread | [Next in Thread] |