[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: fscanf transposes matrix
From: |
Paul Kienzle |
Subject: |
Re: fscanf transposes matrix |
Date: |
Wed, 30 Jan 2002 10:31:35 -0500 |
Octave follows Matlab's broken implementation of scanf, which fills
the matrix by column rather than by row. E.g.,
> sscanf("0 1 2\n3 4 5","%f",[2 3])
ans =
0 2 4
1 3 5
This is not what you would expect from looking at the file:
0 1 2
3 4 5
When using fscanf to read from a file, you will invariably do so
like
> fscanf(fid,"%f",[nc,nr])'
so it is consistent to call it like this, but with nr == Inf.
The case when you have a fixed number of rows but an unknown number of
columns you must handle with something like:
> x = fscanf(fid,"%f"); x = reshape(x,length(x)/nr,nr)'
Unfortunately the behaviour of scanf can't be fixed without breaking
compatibility with existing Octave code and with ported Matlab code, both
of which depends on this behaviour.
Paul Kienzle
address@hidden
On Wed, Jan 30, 2002 at 03:39:43PM +0100, Martijn Brouwer wrote:
> Hi,
> I am (quite) new to octave and I am looking for a way to read an entire
> matrix of measurement data from a file at once.
> I now use a=fscanf(file,"%f%f",[2,Inf]) to read a file containing two
> columns of numbers, but the resulting matrix is transposed compared to
> the file. Offcourse, this is easily cured with a ', but it is counter
> intuitive. Reading with [Inf,2] would in my opinion be better, but this
> results in an one column matrix.
>
> Any comments?
>
> Bye,
>
> Martijn Brouwer
>
>
>
>
>
> -------------------------------------------------------------
> Octave is freely available under the terms of the GNU GPL.
>
> Octave's home on the web: http://www.octave.org
> How to fund new projects: http://www.octave.org/funding.html
> Subscription information: http://www.octave.org/archive.html
> -------------------------------------------------------------
>
-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.
Octave's home on the web: http://www.octave.org
How to fund new projects: http://www.octave.org/funding.html
Subscription information: http://www.octave.org/archive.html
-------------------------------------------------------------