/* * LIBML library - read, write and manipulate MATLAB MAT-files * Copyright (C) 1994 Michael J. Maurer * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the Free * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * Michael Maurer * Durand Bldg - Room 232 * Stanford, CA 94305-4055 * (415) 723-1024 */ /****************************************************************************** mlread.c Writes Matlab (level 1) format MAT files. Written by Michael Maurer (maurer@nova.stanford.edu), after examples by J. Little in the Matlab 3.5 documentation. Those portions not derived from the Mathworks code are Copyright (c) 1993, Michael Maurer (maurer@nova.stanford.edu). ******************************************************************************/ /* $Log$ */ #include #include #include #include "libml.h" #include "libml.p" #include "libmlver.h" #include "libmisc.h" /****************************************************************************** mlwriteh Writes header only of a MAT-file data object. Returns 0 if the write is successful and 1 if a write error is encountered. On input, 'type' gives the format that the data object is already stored in (see mlcvt() to convert between data types). mlwriteh() will byte-swap the header before writing if 'type' implies a foreign-ordered data object. ******************************************************************************/ int mlwriteh(fp,type,pname,mrows,ncols,imagf) FILE *fp; /* File pointer */ int type; /* Type flag: see reference section of guide */ char *pname; /* pointer to matrix name */ int mrows; /* row dimension */ int ncols; /* column dimension */ int imagf; /* imaginary flag */ { Fmatrix x; int M,O,P,T; int namlen=pname?strlen(pname)+1:0; /* * Create Fmatrix structure from file */ x.type=type; x.mrows=mrows; x.ncols=ncols; x.imagf=imagf; x.namlen=namlen; /* swap header bytes if necessary */ (void)mltype(type,&M,&O,&P,&T); if (mlborder(type) != mlborder(ML_TYPE)) swapb_4((unsigned char *)&x,(unsigned char *)&x,sizeof(Fmatrix)/sizeof(long)); if (fwrite((char *)&x,sizeof(Fmatrix),1,fp) != 1) return 1; /* * Write matrix name to file */ if (namlen>0) if (fwrite(pname,sizeof(char),namlen,fp) != namlen) return 1; return 0; } /****************************************************************************** mlwrited() - writes data only of matlab object. The header must already have been written by mlwriteh. The mlwrited() routine returns 0 if the write is successful and 1 if a write error is encountered. Author Michael Maurer 10-4-90, after J.N. Little 11-3-86 ******************************************************************************/ int mlwrited(fp,type,mrows,ncols,imagf,preal,pimag) FILE *fp; /* File pointer */ int type; /* Type flag: see reference section of guide */ int mrows; /* row dimension */ int ncols; /* column dimension */ int imagf; /* imaginary flag */ void *preal; /* pointer to real data */ void *pimag; /* pointer to imag data */ { int mn,dsize,M,O,P,T; mn = mrows * ncols; dsize=mltype(type,&M,&O,&P,&T); /* * Write Real part of matrix to file */ if (dsize>0) if (fwrite(preal,dsize,mn,fp) != mn) return 1; /* * Write Imag part of matrix to file,if it exists */ if (imagf) if (dsize>0) if (fwrite(pimag,dsize,mn,fp) != mn) return 1; return 0; } int mlwrite(fp, type, pname, mrows, ncols, imagf, preal, pimag) FILE *fp; /* File pointer */ int type; /* Type flag: Normally 0 for PC, 1000 for Sun, Mac, and */ /* Apollo, 2000 for VAX D-float, 3000 for VAX G-float */ /* Add 1 for text variables. */ /* See LOAD in reference section of guide for more info. */ int mrows; /* row dimension */ int ncols; /* column dimension */ int imagf; /* imaginary flag */ char *pname; /* pointer to matrix name */ void *preal; /* pointer to real data */ void *pimag; /* pointer to imag data */ { int err; err=mlwriteh(fp,type,pname,mrows,ncols,imagf); if (!err) err=mlwrited(fp,type,mrows,ncols,imagf,preal,pimag); return err; }