one input in mex is not read correctly

1 view (last 30 days)
This is the code for mex file I created. mex file reads one input matrix I perfectly fine. but the values of other input matrix X are not correct.. I am not getting what is the issue.. I have attached the mat file for X(A.mat) and I(I.mat). the input function call pattern is mex name(1,2,4,3,3,I',A);
void IDWT(double z[5],float X[20][4], double row,double col, double of,double nv,double S, double I[9][8])
for (i = 0; i < 20; i++){
for( j = 0 ; j <4 ; j++ ){
printf("%f-", X[i][j]);}
for (i = 0; i < 9; i++){
for( j = 0 ; j <8 ; j++ ){
printf("%f-", I[i][j]);}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
double row; double col; double of; double nv; double S; double *I; float *X;
double *Z;
row = mxGetScalar(prhs[0]);
col = mxGetScalar(prhs[1]);
of = mxGetScalar(prhs[2]);
nv = mxGetScalar(prhs[3]);
S = mxGetScalar(prhs[4]);
I= mxGetPr(prhs[5]);
X = mxGetPr(prhs[6]);
plhs[0] = mxCreateDoubleMatrix(1,nv,mxREAL); // I put nv=5 for input,keeping in mind the size of output
Z = mxGetPr(plhs[0]);
IDWT(Z,X,row,col, of, nv, S, I);

Accepted Answer

James Tursa
James Tursa on 2 Jun 2015
Edited: James Tursa on 2 Jun 2015
1) A.mat contains a double precision variable, but in your argument list you have X declared as a float (single precision). Change that float to double.
2) A.mat contains a 20x4 matrix. The memory layout of this matrices is column-ordered. I.e., the 1st column is in memory first, followed in memory by the 2nd column, followed in memory by the 3rd column, etc. But C 2D matrices are assumed to be in memory in row order. I.e., a [20][4] matrix in C would have the 1st row in memory first, followed in memory by the 2nd row, followed in memory by the 3rd row, etc. This is not going to match up with the MATLAB memory order. If you compare your printed output you will find that, e.g., A(2,1) does not match up with X[1][0] as you might have expected. You need to think of the [ ][ ] indexing as backwards from the MATLAB indexing in order to get things to work out properly. So either you need to pass in the transpose A' like you did with I', or you need to reverse the indexing in the C routine.

Sign in to comment.

More Answers (0)


Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!