writeBinaryArray_mex_c

PURPOSE ^

C/C++ source

SYNOPSIS ^

C/C++ source

DESCRIPTION ^

C/C++ source

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 /*=================================================================
0002  * Mex function to write sparse matrix in PETSc binary format. 
0003  * USAGE: writePetscBinaryMat(filename,A'), where
0004  *          filename: string containing name of file to write to
0005  *          A: sparse matrix to write out.
0006  * IMPORTANT: To write out matrix A you MUST pass A' (A transpose)
0007  *            as the 2d argument. This is because matlab stores 
0008  *            sparse matrices internally in column major format. 
0009  *            This is exactly the opposite of what is needed for 
0010  *            writing out a PETSc binary file.
0011  *=================================================================*/
0012 
0013 #include <stdio.h>
0014 #include <string.h>
0015 #include "mex.h"
0016 
0017  
0018 /* mexFunction is the gateway routine for the MEX-file. */ 
0019 void
0020 mexFunction( int nlhs, mxArray *plhs[],
0021              int nrhs, const mxArray *prhs[] )
0022 {
0023  double  *pr; 
0024   int   m,i,j,k,p=1;
0025   int* n;
0026   FILE *io;
0027   size_t one = 1;
0028   int ARRAY_FILE_COOKIE=1;
0029   int CELLARRAY_FILE_COOKIE=0;
0030   (void) nlhs;     /* unused parameters */
0031   (void) plhs;
0032    int class,cm,*cn,index;
0033    mxArray* cell;
0034   char *filename;
0035   int   buflen,status;
0036     
0037   /* Check for proper number of arguments. */
0038   if (nrhs != 2) 
0039     mexErrMsgTxt("Two inputs required.");
0040 
0041   /* First argument must be a string. */
0042   if (mxIsChar(prhs[0]) != 1)
0043     mexErrMsgTxt("First argument must be a string.");
0044 
0045 
0046   /* Get the length of the input string. */
0047   buflen = (mxGetM(prhs[0]) * mxGetN(prhs[0])) + 1;
0048 
0049   /* Allocate memory for input and output strings. */
0050   filename = mxCalloc(buflen, sizeof(char));
0051 
0052   /* Copy the string data from prhs[0] into a C string 
0053    * filename. */
0054   status = mxGetString(prhs[0], filename, buflen);
0055   if (status != 0) 
0056     mexWarnMsgTxt("Not enough space. String is truncated.");
0057     
0058  class=mxGetClassID(prhs[1]);  
0059  
0060  if(class==mxCELL_CLASS){
0061  /*mexPrintf("Cell Array \n");*/
0062  cm=mxGetNumberOfDimensions(prhs[1]);
0063  /*mexPrintf("%i\t",cm);*/
0064  cn=mxGetDimensions(prhs[1]);
0065  for(j=0;j/*mexPrintf("%i\t",cn[0]);
0067  mexPrintf("%i\n",cn[1]);*/
0068  int ind[2];
0069  ind[1]=0;
0070  ind[0]=0;
0071  io = fopen(filename,"w");
0072  fwrite (&CELLARRAY_FILE_COOKIE,sizeof(int),one,io);
0073  fwrite (&cm,sizeof(int),one,io);
0074  fwrite (cn,sizeof(int),cm,io); 
0075  fwrite (&p,sizeof(int),one,io);
0076  
0077  for(i=0;ifor(k=0;k/*mexPrintf("%i\t",m);*/
0086  
0087     for(j=0;j/*Writing to file*/ 
0091     fwrite (&m,sizeof(int),one,io);
0092     fwrite (n,sizeof(int),m,io); 
0093     fwrite (&p,sizeof(int),one,io);  
0094     fwrite(pr, sizeof(double),p,io); 
0095    }
0096  ind[0]++;
0097  }
0098  fclose(io);
0099 }
0100  else{ 
0101   pr = mxGetPr(prhs[1]);
0102   
0103   m = mxGetNumberOfDimensions(prhs[1]);
0104   n = mxGetDimensions(prhs[1]); 
0105   
0106   
0107   for(i=0;i"w");
0110   fwrite (&ARRAY_FILE_COOKIE,sizeof(int),one,io);   
0111 /*   mexPrintf("%s=%d\n","MAT_FILE_COOKIE",MAT_FILE_COOKIE); */
0112   fwrite (&m,sizeof(int),one,io);
0113   /*mexPrintf("%s=%d\n","m",m); */
0114 
0115   fwrite (n,sizeof(int),m,io); 
0116   fwrite (&p,sizeof(int),one,io);
0117   
0118  /* for(i=0;i<m;i++){
0119    mexPrintf("%i\n",*(n+i));}   
0120    mexPrintf("%s=%d\n","p",p);*/ 
0121    
0122   fwrite(pr, sizeof(double),p,io);
0123   /*for(i=0;i<p;i++){mexPrintf("%g\t",*(pr+i));}*/
0124   
0125   fclose(io);
0126 }
0127 
0128   return;
0129 }

Community support and wiki are available on Redmine. Last update: 18-Apr-2019.