home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / mesch12a.zip / matlab.c < prev    next >
C/C++ Source or Header  |  1994-01-13  |  5KB  |  197 lines

  1.  
  2. /**************************************************************************
  3. **
  4. ** Copyright (C) 1993 David E. Steward & Zbigniew Leyk, all rights reserved.
  5. **
  6. **                 Meschach Library
  7. ** 
  8. ** This Meschach Library is provided "as is" without any express 
  9. ** or implied warranty of any kind with respect to this software. 
  10. ** In particular the authors shall not be liable for any direct, 
  11. ** indirect, special, incidental or consequential damages arising 
  12. ** in any way from use of the software.
  13. ** 
  14. ** Everyone is granted permission to copy, modify and redistribute this
  15. ** Meschach Library, provided:
  16. **  1.  All copies contain this copyright notice.
  17. **  2.  All modified copies shall carry a notice stating who
  18. **      made the last modification and the date of such modification.
  19. **  3.  No charge is made for this software or works derived from it.  
  20. **      This clause shall not be construed as constraining other software
  21. **      distributed on the same medium as this software, nor is a
  22. **      distribution fee considered a charge.
  23. **
  24. ***************************************************************************/
  25.  
  26.  
  27. /*
  28.     This file contains routines for import/exporting data to/from
  29.         MATLAB. The main routines are:
  30.             MAT *m_save(FILE *fp,MAT *A,char *name)
  31.             VEC *v_save(FILE *fp,VEC *x,char *name)
  32.             MAT *m_load(FILE *fp,char **name)
  33. */
  34.  
  35. #include        <stdio.h>
  36. #include        "matrix.h"
  37. #include    "matlab.h"
  38.  
  39. static char rcsid[] = "$Id: matlab.c,v 1.7 1994/01/13 05:30:09 des Exp $";
  40.  
  41. /* m_save -- save matrix in ".mat" file for MATLAB
  42.     -- returns matrix to be saved */
  43. MAT     *m_save(fp,A,name)
  44. FILE    *fp;
  45. MAT     *A;
  46. char    *name;
  47. {
  48.     int     i;
  49.     matlab  mat;
  50.  
  51.     if ( ! A )
  52.         error(E_NULL,"m_save");
  53.  
  54.     mat.type = 1000*MACH_ID + 100*ORDER + 10*PRECISION + 0;
  55.     mat.m = A->m;
  56.     mat.n = A->n;
  57.     mat.imag = FALSE;
  58.     mat.namlen = (name == (char *)NULL) ? 1 : strlen(name)+1;
  59.  
  60.     /* write header */
  61.     fwrite(&mat,sizeof(matlab),1,fp);
  62.     /* write name */
  63.     if ( name == (char *)NULL )
  64.         fwrite("",sizeof(char),1,fp);
  65.     else
  66.         fwrite(name,sizeof(char),(int)(mat.namlen),fp);
  67.     /* write actual data */
  68.     for ( i = 0; i < A->m; i++ )
  69.         fwrite(A->me[i],sizeof(Real),(int)(A->n),fp);
  70.  
  71.     return A;
  72. }
  73.  
  74.  
  75. /* v_save -- save vector in ".mat" file for MATLAB
  76.     -- saves it as a row vector
  77.     -- returns vector to be saved */
  78. VEC     *v_save(fp,x,name)
  79. FILE    *fp;
  80. VEC     *x;
  81. char    *name;
  82. {
  83.     matlab  mat;
  84.  
  85.     if ( ! x )
  86.         error(E_NULL,"v_save");
  87.  
  88.     mat.type = 1000*MACH_ID + 100*ORDER + 10*PRECISION + 0;
  89.     mat.m = x->dim;
  90.     mat.n = 1;
  91.     mat.imag = FALSE;
  92.     mat.namlen = (name == (char *)NULL) ? 1 : strlen(name)+1;
  93.  
  94.     /* write header */
  95.     fwrite(&mat,sizeof(matlab),1,fp);
  96.     /* write name */
  97.     if ( name == (char *)NULL )
  98.         fwrite("",sizeof(char),1,fp);
  99.     else
  100.         fwrite(name,sizeof(char),(int)(mat.namlen),fp);
  101.     /* write actual data */
  102.     fwrite(x->ve,sizeof(Real),(int)(x->dim),fp);
  103.  
  104.     return x;
  105. }
  106.  
  107. /* d_save -- save double in ".mat" file for MATLAB
  108.     -- saves it as a row vector
  109.     -- returns vector to be saved */
  110. double    d_save(fp,x,name)
  111. FILE    *fp;
  112. double    x;
  113. char    *name;
  114. {
  115.     matlab  mat;
  116.     Real x1 = x;
  117.  
  118.     mat.type = 1000*MACH_ID + 100*ORDER + 10*PRECISION + 0;
  119.     mat.m = 1;
  120.     mat.n = 1;
  121.     mat.imag = FALSE;
  122.     mat.namlen = (name == (char *)NULL) ? 1 : strlen(name)+1;
  123.  
  124.     /* write header */
  125.     fwrite(&mat,sizeof(matlab),1,fp);
  126.     /* write name */
  127.     if ( name == (char *)NULL )
  128.         fwrite("",sizeof(char),1,fp);
  129.     else
  130.         fwrite(name,sizeof(char),(int)(mat.namlen),fp);
  131.     /* write actual data */
  132.     fwrite(&x1,sizeof(Real),1,fp);
  133.  
  134.     return x;
  135. }
  136.  
  137. /* m_load -- loads in a ".mat" file variable as produced by MATLAB
  138.     -- matrix returned; imaginary parts ignored */
  139. MAT     *m_load(fp,name)
  140. FILE    *fp;
  141. char    **name;
  142. {
  143.     MAT     *A;
  144.     int     i;
  145.     int     m_flag, o_flag, p_flag, t_flag;
  146.     float   f_temp;
  147.     Real    d_temp;
  148.     matlab  mat;
  149.  
  150.     if ( fread(&mat,sizeof(matlab),1,fp) != 1 )
  151.         error(E_FORMAT,"m_load");
  152.     if ( mat.type >= 10000 )    /* don't load a sparse matrix! */
  153.         error(E_FORMAT,"m_load");
  154.     m_flag = (mat.type/1000) % 10;
  155.     o_flag = (mat.type/100) % 10;
  156.     p_flag = (mat.type/10) % 10;
  157.     t_flag = (mat.type) % 10;
  158.     if ( m_flag != MACH_ID )
  159.         error(E_FORMAT,"m_load");
  160.     if ( t_flag != 0 )
  161.         error(E_FORMAT,"m_load");
  162.     if ( p_flag != DOUBLE_PREC && p_flag != SINGLE_PREC )
  163.         error(E_FORMAT,"m_load");
  164.     *name = (char *)malloc((unsigned)(mat.namlen)+1);
  165.     if ( fread(*name,sizeof(char),(unsigned)(mat.namlen),fp) == 0 )
  166.         error(E_FORMAT,"m_load");
  167.     A = m_get((unsigned)(mat.m),(unsigned)(mat.n));
  168.     for ( i = 0; i < A->m*A->n; i++ )
  169.     {
  170.         if ( p_flag == DOUBLE_PREC )
  171.             fread(&d_temp,sizeof(double),1,fp);
  172.         else
  173.         {
  174.             fread(&f_temp,sizeof(float),1,fp);
  175.             d_temp = f_temp;
  176.         }
  177.         if ( o_flag == ROW_ORDER )
  178.             A->me[i / A->n][i % A->n] = d_temp;
  179.         else if ( o_flag == COL_ORDER )
  180.             A->me[i % A->m][i / A->m] = d_temp;
  181.         else
  182.             error(E_FORMAT,"m_load");
  183.     }
  184.  
  185.     if ( mat.imag )         /* skip imaginary part */
  186.     for ( i = 0; i < A->m*A->n; i++ )
  187.     {
  188.         if ( p_flag == DOUBLE_PREC )
  189.             fread(&d_temp,sizeof(double),1,fp);
  190.         else
  191.             fread(&f_temp,sizeof(float),1,fp);
  192.     }
  193.  
  194.     return A;
  195. }
  196.  
  197.