matlab.c 5.4 KB

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