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 whose 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 integer 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);
}