ReadBinaryArray_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 mexFunction(int nlhs, mxArray *plhs[],
0020              int nrhs, const mxArray *prhs[] )
0021          {
0022  char *file;
0023  int length,status;
0024  int ndims,*dim,size,*cookie;
0025  double *vals;
0026  
0027  if (mxIsChar(prhs[0]) != 1)
0028     {mexErrMsgTxt("Input must be a string.");}
0029  else{
0030     length=(mxGetM(prhs[0]) * mxGetN(prhs[0])) + 1;
0031     file=mxCalloc(length,sizeof(char));
0032     status = mxGetString(prhs[0], file, length);
0033     if (status != 0) 
0034        {mexWarnMsgTxt("Not enough space. String is truncated.");}
0035  }
0036  
0037  FILE *TENSOR=fopen(file,"r");
0038  fread(&cookie,sizeof(int),1,TENSOR);
0039  fread(&ndims,sizeof(int),1,TENSOR);
0040  dim=mxCalloc(ndims,sizeof(int));
0041  fread(dim,sizeof(int),ndims,TENSOR);
0042  fread(&size,sizeof(int),1,TENSOR);
0043  vals=mxCalloc(size,sizeof(double));
0044  fread(vals,sizeof(double),size,TENSOR);  
0045  fclose(TENSOR);
0046  mxArray * pA;
0047  plhs[0]=mxCreateNumericArray(ndims,dim,mxDOUBLE_CLASS,mxREAL);
0048  mxSetPr(plhs[0],vals);
0049   return;
0050 }

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