Reading files in Octave

John W. Eaton jwe at octave.org
Wed Mar 11 12:53:15 CDT 2009


On 11-Mar-2009, Ivan Sutoris wrote:

| On Wed, Mar 11, 2009 at 5:59 PM, Samuel H. Dupree, Jr.
| <sdupree at speakeasy.net> wrote:
| > I'm running Octave version 3.0.3 under Mac OS 10.4.11 on a Mac PowerPC G4.
| > I'm attempting to read a file of spherical harmonic coefficients of the
| > form:
| >
| >  2   0   0.202150907893000000e-03   0.000000000000000000e+00
| >  3   0   0.121260448837000000e-04   0.000000000000000000e+00
| >  4   0  -0.145383007072000000e-06   0.000000000000000000e+00
| >  3   1   0.307082741328000000e-04   0.561066891941000000e-05
| >  4   1  -0.717780149806000000e-05   0.294743374914000000e-05
| >  2   2   0.223035130900000000e-04   0.000000000000000000e+00
| >  3   2   0.488840471683000000e-05   0.168743052295000000e-05
| >  4   2  -0.143951838385000000e-05  -0.288437212720000000e-05
| >  2   3   0.000000000000000000e+00   0.000000000000000000e+00
| >  3   3   0.143603108489000000e-05  -0.334354467700000000e-06
| >  4   3  -0.854788154819000000e-07  -0.788967312839000000e-06
| >  2   4   0.000000000000000000e+00   0.000000000000000000e+00
| >  3   4   0.000000000000000000e+00   0.000000000000000000e+00
| >  4   4  -0.154903893130000000e-06   0.564041555720000000e-07
| >
| > In Fortran I can read and store each of the cosine (Cij) and sine (Sij)
| > coefficients using:
| >
| > read( unit = lulugr, fmt = *, iostat = iret )  i, j, Cl(i,j), Sl(i,j)

This works to read the entire file?  Without any kind of loop?

| > In Octave I attempted to do the same thing using fscanf:
| >
| >  [ j, k, Cl(j+1,k+1), Sl(j+1,k+1), KOUNT ] = fscanf( gravfile, "%i %i %e
| > %e", "C" );
| >
| > Octave returns an error message stating that k is undefined.
| >
| >
| > The questions I have for this list are: (1) what am I doing wrong, and (2)
| > how do I get Octave to input data from an ASCII file like the one
| > illustrated above?
| >
| >
| > Thanks in advance,
| >
| > Sam Dupree.
| 
| k appears only in indices of left hand variables, the fscanf usage
| itself seems to be ok. I don't know much about IO in Fortran, but I
| think in Octave you need to go through each line individually with
| cycle, this seems to work:
| 
| fid = fopen("testdata.txt");
| [a1 a2 a3 a4] = fscanf(fid, '%i %i %e %e', 'C');
| while (~feof(fid))
| 	j = a1;
| 	k = a2;
| 	Cl(j+1,k+1) = a3;
| 	Sl(j+1,k+1) = a4;
| 	[a1 a2 a3 a4] = fscanf(fid, '%i %i %e %e', 'C');
| end
| fclose(fid);

It would probably be a lot faster to just read all the data at once,
then reshape and rename as needed:

  fid = fopen ("foo.dat");
  tmp = fscanf (fid, "%f").';
  fclose (fid);
  j = tmp(:,1);pppppp
  k = tmp(:,2);
  Cl = ...
  Sl = ...

I'm having trouble understanding exactly how i,j and j,k in your
above example are supposed to be used as indices to Cl and Sl, and
what you expect the final sizes to be, so I'm not sure precisely how
to take the 3rd and 4th columns of TMP and turn it into Cl an Sl.

jwe



More information about the Help-octave mailing list