0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include <stdio.h>
0014 #include <string.h>
0015 #include "mex.h"
0016
0017 #define MAT_OUT plhs[0]
0018
0019
0020 void mexFunction(int nlhs, mxArray *plhs[],
0021 int nrhs, const mxArray *prhs[] )
0022 {
0023 char *file;
0024 int length,status;
0025 int *cookie,*ndims,*dim,*size,*vals;
0026 int ii,result;
0027 int *matout;
0028
0029 if (mxIsChar(prhs[0]) != 1)
0030 {mexErrMsgTxt("Input must be a string.");}
0031 else{
0032 length=(mxGetM(prhs[0]) * mxGetN(prhs[0])) + 1;
0033 file=mxCalloc(length,sizeof(char));
0034 status = mxGetString(prhs[0], file, length);
0035 if (status != 0)
0036 {mexWarnMsgTxt("Not enough space. String is truncated.");}
0037 }
0038
0039 FILE *TENSOR = fopen(file,"r");
0040
0041 cookie = mxCalloc(1,sizeof(int));
0042 if (cookie == NULL) {mexErrMsgTxt("Memory allocation error in ReadBinaryIntArray_mex !");}
0043 result = fread(cookie,sizeof(int),1,TENSOR);
0044 if (result != 1) {mexErrMsgTxt("Reading file problem in ReadBinaryIntArray_mex !");}
0045
0046
0047
0048 "Memory allocation error in ReadBinaryIntArray_mex !"
0049
0050 if (result != 1) {mexErrMsgTxt("Reading file problem in ReadBinaryIntArray_mex !");}
0051
0052
0053
0054 "Memory allocation error in ReadBinaryIntArray_mex !"
0055
0056 if (result != ndims[0]) {mexErrMsgTxt("Reading file problem in ReadBinaryIntArray_mex !");}
0057
0058 "dim[%i]=%i\n"
0059
0060
0061 size = mxCalloc(1,sizeof(int));
0062 if (size == NULL) {mexErrMsgTxt("Memory allocation error in ReadBinaryIntArray_mex !");}
0063 result = fread(size,sizeof(int),1,TENSOR);
0064 if (result != 1) {mexErrMsgTxt("Reading file problem in ReadBinaryIntArray_mex !");}
0065
0066
0067
0068 "Memory allocation error in ReadBinaryIntArray_mex !"
0069
0070 if (result != size[0]) {mexErrMsgTxt("Reading file problem in ReadBinaryIntArray_mex !");}
0071
0072 "vals[%i]=%i\n"
0073
0074
0075 fclose(TENSOR);
0076
0077
0078 MAT_OUT = mxCreateNumericArray(ndims[0],dim,mxINT32_CLASS,mxREAL);
0079 matout = mxGetData(MAT_OUT);
0080 for (ii = 0;ii < size[0];ii++) {
0081 matout[ii] = vals[ii];
0082 }
0083
0084
0085
0086 mxFree (cookie);
0087 mxFree (dim);
0088 mxFree (ndims);
0089 mxFree (size);
0090 mxFree (vals);
0091
0092 return;
0093 }