home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / softsys / matlab / 209 < prev    next >
Encoding:
Text File  |  1993-01-28  |  4.1 KB  |  123 lines

  1. Newsgroups: comp.soft-sys.matlab
  2. Path: sparky!uunet!charon.amdahl.com!amdahl!rtech!sgiblab!spool.mu.edu!sol.ctr.columbia.edu!destroyer!cs.ubc.ca!news.UVic.CA!sirius!Brad.Hedstrom
  3. From: Brad.Hedstrom@engr.UVic.CA (Brad  Hedstrom)
  4. Subject: Re: Some Questions on Matlab
  5. In-Reply-To: sumargoh@widget.fiu.edu's message of Tue, 26 Jan 1993 16:25:01 GMT
  6. Message-ID: <BRAD.HEDSTROM.93Jan27104521@active.UVic.CA>
  7. Lines: 109
  8. Sender: news@sol.UVic.CA
  9. Nntp-Posting-Host: active.uvic.ca
  10. Organization: University of Victoria, Victoria, BC, Canada
  11. References: <C1Gyxq.9p1@fiu.edu>
  12. Date: Wed, 27 Jan 93 18:45:28 GMT
  13.  
  14. On Tue, 26 Jan 1993 16:25:01 GMT, sumargoh@widget.fiu.edu said:
  15. >2.  The MatLab package comes with some C source codes to read binary
  16. >files and convert them to numbers.  I tried to implement a binary to
  17. >number conversion using the above mentioned C source codes with no
  18. >avail.  Has anyone out there ever written such routine in C and would
  19. >like to share?
  20.  
  21. Here are two C subroutines for writing floating-point and integer
  22. Matlab binary data files. I've been using them for years on Suns and
  23. Macs. They're pretty much right out of the Matlab manual if I remember
  24. correctly .
  25.  
  26. file matlabbin.h
  27. extern void fmatsave();
  28. extern void imatsave();
  29.  
  30. file matlabbin.c
  31. typedef struct{
  32.     long int type;    /* type of file (MOPT) */
  33.     long int mrows;    /* number of rows of variable */
  34.     long int ncols;    /* number of colums of variable */
  35.     long int imagf;    /* 0 => no imaginary part, 1 => imaginary part */
  36.     long int namlen;/* length of name string (including NULL) */
  37. } Fmatrix;
  38.  
  39. # include <stdio.h>
  40. # include <string.h>
  41.  
  42. /* This routine writes floating point data (matrix or vector) 
  43.    in the matlab file format */
  44.  
  45. void fmatsave(real, imag, rowspace, colspace, name, fileptr)
  46. float *real;    /* real part of vector */
  47. float *imag;    /* imaginary part of vector */
  48. int rowspace;    /* number of rows of variable */
  49. int colspace;    /* number of colums of variable */
  50. char *name;    /* pointer to name of variable */
  51. FILE *fileptr;    /* pointer to destination file */
  52.  
  53. {
  54.     int mn;        /* row/column counter */
  55.     Fmatrix x;    /* MATLAB file data structure */
  56.  
  57.     /* define constants */
  58.     x.type = 1010;    /* Sun ID, column wise, float precision, numeric */
  59.  
  60.     /* define the MATLAB data structure */
  61.     x.mrows = rowspace;
  62.     x.ncols = colspace;
  63.     if (imag == NULL){        /* check for existance of imaginary */
  64.         x.imagf = 0;
  65.     } else {
  66.         x.imagf = 1;
  67.     }
  68.     x.namlen = strlen(name) + 1;    /* add one for NULL not counted */
  69.  
  70.     /* write binary MATLAB file */
  71.     fwrite(&x, sizeof(Fmatrix), 1, fileptr);
  72.     fwrite(name, sizeof(char), x.namlen, fileptr);
  73.     mn = x.mrows * x.ncols;    
  74.     fwrite(real, sizeof(float), mn, fileptr);
  75.     if (x.imagf){
  76.         fwrite(imag, sizeof(float), mn, fileptr);
  77.     }
  78. }
  79.  
  80. /* This routine writes integer data (matrix or vector) 
  81.    in the matlab file format */
  82.  
  83. void imatsave(real, imag, rowspace, colspace, name, fileptr)
  84. int *real;    /* real part of vector */
  85. int *imag;    /* imaginary part of vector */
  86. int rowspace;    /* number of rows of variable */
  87. int colspace;    /* number of colums of variable */
  88. char *name;    /* pointer to name of variable */
  89. FILE *fileptr;    /* pointer to destination file */
  90.  
  91. {
  92.     int mn;        /* row/column counter */
  93.     Fmatrix x;    /* MATLAB file data structure */
  94.  
  95.     /* define constants */
  96.     x.type = 1020;    /* Sun ID, column wise, float precision, numeric */
  97.  
  98.     /* define the MATLAB data structure */
  99.     x.mrows = rowspace;
  100.     x.ncols = colspace;
  101.     if (imag == NULL){        /* check for existance of imaginary */
  102.         x.imagf = 0;
  103.     } else {
  104.         x.imagf = 1;
  105.     }
  106.     x.namlen = strlen(name) + 1;    /* add one for NULL not counted */
  107.  
  108.     /* write binary MATLAB file */
  109.     fwrite(&x, sizeof(Fmatrix), 1, fileptr);
  110.     fwrite(name, sizeof(char), x.namlen, fileptr);
  111.     mn = x.mrows * x.ncols;    
  112.     fwrite(real, sizeof(int), mn, fileptr);
  113.     if (x.imagf){
  114.         fwrite(imag, sizeof(int), mn, fileptr);
  115.     }
  116. }
  117. --
  118. ___________________________________________________________________________
  119. Brad Hedstrom: Grad Student, Research Assistant | Elec. & Comp. Engr. Dept.
  120. Also `Consultant to the Stars' in:              |    University of Victoria
  121. DSP, LabVIEW, Networking, Sun, and Macintosh    |    Victoria, B.C., Canada
  122. Internet: hedstrom@sirius.UVic.CA               |     (a.k.a. Brazil North)
  123.