필터 지우기
필터 지우기

one input in mex is not read correctly

조회 수: 1 (최근 30일)
Abeera Tariq
Abeera Tariq 2015년 6월 2일
댓글: Abeera Tariq 2015년 6월 2일
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
Abeera Tariq
Abeera Tariq 2015년 6월 2일
It is done

댓글을 달려면 로그인하십시오.

추가 답변 (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