#include /* * Matlab binary data file header format: * * type is of the form MOPT * * M = 0 for IEEE Little Endian (PC), 1 for IEEE Big Endian (Sparc) * 2 for VAX D-float, 3 for VAX G-float, 4 for Cray. * * O = 0, reserved for future use. * * P = 0 for double, 1 for float, 2 for long, 3 for short, * 4 for unsigned short, 5 for unsigned char * * T = 0 for matrix, 1 for text, 2 for sparse matrix * * Following the header is the matrix name, the real part (column-wise), * then the imaginary part, if any. */ typedef struct { long type; /* type */ long rows; /* row dimension */ long cols; /* column dimension */ long imag; /* flag for imaginary part */ long namelen; /* name length, including NULL */ } Matlab_header; double a[2][3] = { 1, 2, 3, 4, 5, 6 }; main() { Matlab_header h; h.type = 1000; h.rows = 2; h.cols = 3; h.imag = 0; h.namelen = 2; fwrite( &h, sizeof( Matlab_header), 1, stdout); fwrite( "a", sizeof( char), 2, stdout); fwrite( a, sizeof(double), h.rows * h.cols, stdout); }