Octave binary format

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Octave binary format

Ted.Harding
Hello All:

A question in an earlier posting may have got lost in the discussion
about speed of fprintf.

The question was: can anyone point me to a specification of the
octave binary-save format (or perhaps the MatLab one)?

With thanks,
Ted.                                     ([hidden email])

Reply | Threaded
Open this post in threaded view
|

Re: Octave binary format

John Eaton-4
[hidden email] (Ted Harding) wrote:

: Hello All:
:
: A question in an earlier posting may have got lost in the discussion
: about speed of fprintf.
:
: The question was: can anyone point me to a specification of the
: octave binary-save format (or perhaps the MatLab one)?

The Octave binary format is briefly described in the comments before
the function read_binary_data() in load-save.cc.  The value of the
`magic number' mentioned there can be either Octave-1-L or Octave-1-B,
which serves to identify that the file is an Octave binary data file,
version 1, and that it was written on either a little-endian or
big-endian machine.

The Matlab data file format is described in some Matlab manual.  I
think it is the `External Reference Guide'.

Code for reading and writing both of these formats is in load-save.cc.
(Yes, it would be useful to have these functions in a separate library
that could be called from C, C++, etc.).

jwe

Reply | Threaded
Open this post in threaded view
|

Re: Octave binary format

Christoph Mecklenbraeuker
In reply to this post by Ted.Harding

Dear All,

Ted Harding ([hidden email]) wrote:

> The question was: can anyone point me to a specification of the
> octave binary-save format (or perhaps the MatLab one)?

Here is some info about Matlab M-files: (Version 4.2)
---
MAT-File Structure

This text describes the internal structure of Level 1.0 MAT-files.

A MAT-file may contain one or more Matrices. The Matrices are written
sequentially on disk, with the bytes forming a continuous stream. Each
Matrix starts with a fixed length 20-byte header that consists of five
long (4-byte) integers:

   type      the type flag contains an integer whoses decimal digits
             encode storage information. If the integer is represented
             as MOPT where M is the thousands digit, O is the hundreds
             digit, P is the tens digit, and T is the ones digit, then:

                M  indicates the numeric format of binary numbers on
                   the machine that wrote the file. Use this table to
                   determine the number to use for your machine:

                        0 IEEE Little Endian (PC, 386, 486, DEC Risc)
                        1 IEEE Big Endian (Mac, SPARC, Apollo, SGI, HP
                          9000/300, other Motorola)
                        2 VAX D-float
                        3 VAX G-float
                        4 Cray

                O  is always zero and is reserved for future use.

                P  indicates which format the data is stored in
                   according to the following table:

                        0 double-prec (64-bit) float numbers
                        1 single-prec (32-bit) float numbers
                        2 32-bit signed integers
                        3 16-bit signed integers
                        4 16 bit unsigned integers
                        5 8-bit unsigned integers

                T  indicates the Matrix type according to the
                   following table:

                        0 Numeric (Full) Matrix
                        1 Text Matrix
                        2 Sparse Matrix

                   Note that the elements of a text Matrix are stored
                   as floating point numbers between 0 and 255
                   representing ASCII-encoded characters.

   mrows     The row dimension contains an integer with the number of
             rows in the Matrix.

   ncols     The column dimension contains an integer with the number of
             columns in the Matrix.

   imagf     The imaginary flag is an integers whose value is either 0
             or 1. If 1, then the Matrix has an imaginary part. If 0,
             there is only real data.

   namlen    The name length contains an integer with 1 plus the
             length of the Matrix name.

Immediately following the fixed length header is the data whoses
length is dependent on the variables in the fixed length header:

   name      The Matrix name consists of namlen ASCII bytes, the last
             one of which must be a null character (encoded as 0).

   real      Real part of the Matrix consists of mrows * ncols numbers
             in the format specified by the P element of the type flag.
             The data is stored column-wise such that the second
             column follows the first column, etc.

   imag      Imaginary part of the Matrix, if any. If the imaginary
             flag imagf is nonzero, the imaginary part of a Matrix is
             here. It is stored in the same manner as the real data.

This structure is repeated for each Matrix stored in the file.

The following C language code demonstrates how to write a single
Matrix to disk in Level 1.0 MAT-file format.

   typedef struct {
        long type; /* type */
        long mrows;     /* row dimension */
        long ncols; /* column dimension */
        long imagf; /* flag indicating imag part */
        long manlen; /* name length (including NULL) */
   } Fmatrix;

   char *pname; /* pointer to matrix name */
   double *pr; /* pointer to real data */
   double *pi; /* pointer to imag data */
   FILE *fp;
   Fmatrix x;
   int mn;

   fwrite(&x, sizeof(Fmatrix), 1, fp);
   fwrite(pname, sizeof(char), x.namlen, fp);
   mn = x.mrows * x.ncols;
   fwrite(pr, sizeof(double), mn, fp);
   if(x.imagf)
     {
       fwrite(pi, sizeof(double), mn, fp);
     }

-----
Cheers,
Christoph