one input in mex is not read correctly

1 次查看(过去 30 天)
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]);}
printf("\n");
}
for (i = 0; i < 9; i++){
for( j = 0 ; j <8 ; j++ ){
printf("%f-", I[i][j]);}
printf("\n");
}
printf("\n");
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
//DECLARING ALL THE ARGUMENTS
double row; double col; double of; double nv; double S; double *I; float *X;
//DOUBLE PRECISION CORRESPONDANCE OF THE OUTPUT
double *Z;
row = mxGetScalar(prhs[0]);
col = mxGetScalar(prhs[1]);
of = mxGetScalar(prhs[2]);
nv = mxGetScalar(prhs[3]);
S = mxGetScalar(prhs[4]);
// SPECIAL CASE FOR ARRAYS
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);
}

采纳的回答

James Tursa
James Tursa 2015-6-2
编辑:James Tursa 2015-6-2
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.
  5 个评论
Abeera Tariq
Abeera Tariq 2015-6-2
James you are a life saver. Thanks A lotttttttttttttt

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Write C Functions Callable from MATLAB (MEX Files) 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by