[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
## Re: Reading files in Octave

**From**: |
Przemek Klosowski |

**Subject**: |
Re: Reading files in Octave |

**Date**: |
Wed, 11 Mar 2009 14:05:05 -0400 (EDT) |

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
....
One not very elegant way is:
a=fscanf(fopen('gravfile'),"%f")
i=a(1:4:end), j=a(2:4:end)
Cl=sparse(i+1,j+1,a(3:4:end))
Sl=sparse(i+1,j+1,a(4:4:end))
It would have been nicer but strangely less readable to:
a=fscanf(fopen('gravfile'),"%f");
a=reshape(a,4,length(a)/4)';
Cl=sparse(a(:,1)+1,a(:,2)+1,a(:,3))
Sl=sparse(a(:,1)+1,a(:,2)+1,a(:,4))
I am sure that others will show how to write it more nicely--
for instance, I don't remember if there's a way to read
directly into a 4xN shape, so that reshape() is not needed.
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.
I think that's because Octave tries to read the whole file in the right-hand
statement, rather than implicitly iterating, setting variables like Fortran.