[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Reading files in Octave
From: |
Ivan Sutoris |
Subject: |
Re: Reading files in Octave |
Date: |
Wed, 11 Mar 2009 18:34:45 +0100 |
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)
>
> 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);
Regards
Ivan Sutoris
Re: Reading files in Octave, Przemek Klosowski, 2009/03/11