Hi everyone,
I'm new to the C programmation with the API library in Matlab and I'm trying to read and access a mat file data which is a 3d array. In my C code "p" is a pointer to the 3d array which values I'm trying to print, but when I compile (mex MyCode.c) I have a errors, here's my C code:
#include "mex.h"#include "matrix.h"#include "mat.h" void mexFunction( int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[] ) { MATFile *pmat; double x; mxArray *pa; int n; const char *name; const char **dir; int ndir; int i,c; int num_of_dim, num_of_fields, rows, col, bands,r,cc,b; const int *dim_array; mxArray ***p; /* * Open file to get directory */ pmat = matOpen("data3.mat", "r"); if (pmat == NULL) { printf("Error creating file \n"); printf("(Do you have write permission in this directory?)\n"); return(EXIT_FAILURE); } /* * get directory of MAT-file */ dir = (const char **)matGetDir(pmat, &ndir); if (dir == NULL) { printf("Error reading directory of file "); return(EXIT_FAILURE); } mxFree(dir); if (matClose(pmat) != 0) { printf("Error closing file \n"); return(EXIT_FAILURE); } pmat = matOpen("data3.mat", "r"); if (pmat == NULL) { printf("Error reopening file \n"); return(EXIT_FAILURE); } /* Read in each array. */ printf("\nReading in the actual array contents:\n"); for (i=0; i<ndir; i++) { pa = matGetNextVariable(pmat, name); if (pa == NULL) { printf("Error reading in file \n"); return(EXIT_FAILURE); } } /* Get the number of dimensions in array */ num_of_dim=mxGetNumberOfDimensions(pa); dim_array = mxGetDimensions(pa); printf("the number of dimensions are %d \n",num_of_dim); for (c=0; c<num_of_dim; c++) { mexPrintf("%d\n", *(dim_array+c)); } rows = *(dim_array); col = *(dim_array+1); bands = *(dim_array+2); p=mxGetData(pmat); /* Display the values of the mat file */ for (r=0;r<rows;r++,printf("\n")) { for(cc=0;cc<col;cc++,printf("\n")) { for(b=0;b,<bands;b++,printf("\n")) { mexPrintf("%d\n",*(p + col*bands*r + bands*cc + b)); } } } }
I will be really grateful to know what's wrong with my C code.
Thanks.
-J
Best Answer