From: Brad.Hedstrom@engr.UVic.CA (Brad  Hedstrom)
Subject: Re: Some Questions on Matlab
Organization: University of Victoria, Victoria, BC, Canada
Date: Wed, 27 Jan 93 18:45:28 GMT

On Tue, 26 Jan 1993 16:25:01 GMT, sumargoh@widget.fiu.edu said:
>2.  The MatLab package comes with some C source codes to read binary
>files and convert them to numbers.  I tried to implement a binary to
>number conversion using the above mentioned C source codes with no
>avail.  Has anyone out there ever written such routine in C and would
>like to share?

Here are two C subroutines for writing floating-point and integer
Matlab binary data files. I've been using them for years on Suns and
Macs. They're pretty much right out of the Matlab manual if I remember
correctly .

file matlabbin.h
extern void fmatsave();
extern void imatsave();

file matlabbin.c
typedef struct{
	long int type;	/* type of file (MOPT) */
	long int mrows;	/* number of rows of variable */
	long int ncols;	/* number of colums of variable */
	long int imagf;	/* 0 => no imaginary part, 1 => imaginary part */
	long int namlen;/* length of name string (including NULL) */
} Fmatrix;

# include <stdio.h>
# include <string.h>

/* This routine writes floating point data (matrix or vector) 
   in the matlab file format */

void fmatsave(real, imag, rowspace, colspace, name, fileptr)
float *real;	/* real part of vector */
float *imag;	/* imaginary part of vector */
int rowspace;	/* number of rows of variable */
int colspace;	/* number of colums of variable */
char *name;	/* pointer to name of variable */
FILE *fileptr;	/* pointer to destination file */

{
	int mn;		/* row/column counter */
	Fmatrix x;	/* MATLAB file data structure */

	/* define constants */
	x.type = 1010;	/* Sun ID, column wise, float precision, numeric */

	/* define the MATLAB data structure */
	x.mrows = rowspace;
	x.ncols = colspace;
	if (imag == NULL){		/* check for existance of imaginary */
		x.imagf = 0;
	} else {
		x.imagf = 1;
	}
	x.namlen = strlen(name) + 1;	/* add one for NULL not counted */

	/* write binary MATLAB file */
	fwrite(&x, sizeof(Fmatrix), 1, fileptr);
	fwrite(name, sizeof(char), x.namlen, fileptr);
	mn = x.mrows * x.ncols;	
	fwrite(real, sizeof(float), mn, fileptr);
	if (x.imagf){
		fwrite(imag, sizeof(float), mn, fileptr);
	}
}

/* This routine writes integer data (matrix or vector) 
   in the matlab file format */

void imatsave(real, imag, rowspace, colspace, name, fileptr)
int *real;	/* real part of vector */
int *imag;	/* imaginary part of vector */
int rowspace;	/* number of rows of variable */
int colspace;	/* number of colums of variable */
char *name;	/* pointer to name of variable */
FILE *fileptr;	/* pointer to destination file */

{
	int mn;		/* row/column counter */
	Fmatrix x;	/* MATLAB file data structure */

	/* define constants */
	x.type = 1020;	/* Sun ID, column wise, float precision, numeric */

	/* define the MATLAB data structure */
	x.mrows = rowspace;
	x.ncols = colspace;
	if (imag == NULL){		/* check for existance of imaginary */
		x.imagf = 0;
	} else {
		x.imagf = 1;
	}
	x.namlen = strlen(name) + 1;	/* add one for NULL not counted */

	/* write binary MATLAB file */
	fwrite(&x, sizeof(Fmatrix), 1, fileptr);
	fwrite(name, sizeof(char), x.namlen, fileptr);
	mn = x.mrows * x.ncols;	
	fwrite(real, sizeof(int), mn, fileptr);
	if (x.imagf){
		fwrite(imag, sizeof(int), mn, fileptr);
	}
}
--
___________________________________________________________________________
Brad Hedstrom: Grad Student, Research Assistant | Elec. & Comp. Engr. Dept.
Also `Consultant to the Stars' in:              |    University of Victoria
DSP, LabVIEW, Networking, Sun, and Macintosh    |    Victoria, B.C., Canada
Internet: hedstrom@sirius.UVic.CA               |     (a.k.a. Brazil North)