help-octave
[Top][All Lists]
Advanced

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

Re: Reading files in Octave


From: Samuel H. Dupree, Jr.
Subject: Re: Reading files in Octave
Date: Wed, 11 Mar 2009 16:09:28 -0400
User-agent: Thunderbird 2.0.0.19 (Macintosh/20081209)

John W. Eaton wrote:
On 11-Mar-2009, Ivan Sutoris wrote:

| On Wed, Mar 11, 2009 at 5:59 PM, Samuel H. Dupree, Jr.
| <address@hidden> 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


  
First, I'd like to say thank you for all the responses I received to my question. Second, to answer
the question raised concerning the read statement depicted in my original posting, the actual
Fortran code snippet to input the degree, order cosine and sine spherical harmonics coefficients
is:

      open( unit = lulugr, file = lungrav_file, status = 'old' )
      rewind   lulugr
      done = .false.
      do   while ( .not. done )
         read( unit = lulugr, fmt = *, iostat = iret )
     1                i, j, Cl(i,j), Sl(i,j)
         if( iret .eq. 0 ) then
            if( j .eq. 0 ) then
               Jl(i)   = Cl(i,j)
               Cl(i,j) = 0.d0
               Sl(i,j) = 0.d0
            end if
         else
            done = iret .ne. 0
         end if
      end do
      close( unit = lulugr )

where the input data went something like:

 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


where the first two columns give the degree and order (i and j respectively) of the cosine and
sine coefficients (Cl(i,j) and Sl(i,j) respectively) in columns three and four. In Fortran, one can
use matrix indices (i and j) loaded in one part of the read statement and used to load data into
a matrix elements (Cl(i,j) or Sl(i,j)) in another part of the read statement. I hope this  explanation
clears things up.


Once again, thanks for the responses.

Sam Dupree.


reply via email to

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