MATLAB: Read and access MAT file data in C

.mat filec

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

This:
const int *dim_array;
should probably be this
const mwSize *dim_array;
Also, this
mxArray ***p;
should probably be this, assuming the array is double class (you should check this in your code)
double *p;
And if the values really are double, then you should be using a format other than %d for printing, since that is for integer types. E.g., this
mexPrintf("%d\n",*(p + col*bands*r + bands*cc + b));
should be something like this instead
mexPrintf("%g\n",*(p + col*bands*r + bands*cc + b));
And this
p=mxGetData(pmat);
should probably be this
p = mxGetData(pa);
And you should have this at the end of your outer loop
mxDestroyArray(pa);
This might not be totally correct either ... seems like you would need to free the name strings as well:
mxFree(dir);
Finally, it is not clear to me which variable you are trying to display here. You read them all in a loop, and then display only the last one? Is that intended?
And then in the following call, I think "name" needs to have memory allocated behind it prior to the call ... I will have to check on this later.
pa = matGetNextVariable(pmat, name);
EDIT:
I checked, and that last line should be this:
pa = matGetNextVariable(pmat, &name);