Mex file. error in passing data.

2 Ansichten (letzte 30 Tage)
amit jha
amit jha am 7 Apr. 2020
Kommentiert: amit jha am 17 Mai 2020
Hello,
i am struggling with my very first Mex file experience.
I wrote a mex function as follows.
file name is 'hello_world.c'
#include <math.h>
#include <matrix.h>
#include <mex.h>
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
double *C, *Node_val;
int l1,l2, v1, v2, k, n, K,ip,jp,j,i1,i2;
double Cijk;
double *CE,*Hash;
// getting rhs pointers for scalars to be used as input
l1 = mxGetScalar(prhs[0]);
l2 = mxGetScalar(prhs[1]);
v1 = mxGetScalar(prhs[2]);
k = mxGetScalar(prhs[3]);
K = mxGetScalar(prhs[4]);
n = mxGetScalar(prhs[5]);
Hash=mxGetPr(prhs[6]);
C=mxGetPr(prhs[7]);
Node_val=mxGetPr(prhs[8]);
//printf("H: %f",Hash);
// create a pointer for output
plhs[0] = mxCreateDoubleMatrix(n, k, mxREAL);
CE = mxGetPr(plhs[0]);
for (v2 = l1-1 ; v2 <=l2-1; v2=v2+1)
{
j=Hash[v2];
if (j <=2 || j >= K-1)
{ i1 = Hash[v2+n];
i2 = Hash[v2+2*n];
ip = 1/2*(j-i1);
jp = 1/2*(j+i1);
Cijk = C[ip+(jp)*(K/2+1)];
ip = 1/2*(j-i2);
jp = 1/2*(j+i2);
CE[v2] = Cijk+C[ip+(jp)*(K/2+1)]+Node_val[v1-1];
printf("CE: %f",CE);
}
}
}
This Matlab code i am calling this mexfunction from is following. I am getting erroneous output as CE[1:3]=[0,8.0362, 8.036, 8.036,........0,0]. I understand that the Hash[v2] is not passing integer to j as j is int and Hash is a double pointer.
Hash=rand(14,3);
n=14;
K=4;
k=1;
l1=2;
l2=4;
v1=1;
C=[4.01808033751942,1.23318934835166,4.17267069084370;0.759666916908419,1.83907788282417,0.496544303257421;2.39916153553658,2.39952525664903,9.02716109915281];
Node_val=ones(n,3);
Node_val(1,:)=[0 0 0];
%CE=ones(n,1);
mex hello_world.c
CE=hello_world(l1,l2,v1,k,K,n,Hash,C,Node_val);
  1 Kommentar
amit jha
amit jha am 17 Mai 2020
thanks Guillaume. the problem was sorted with your suggestions. data type declaration error with variable Hash and float to integer conversion using 1./2 instead of 1/2. these were the basic errors apart from some other indexing errors. thanks a lot..

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Guillaume
Guillaume am 7 Apr. 2020
Bearbeitet: Guillaume am 7 Apr. 2020
Your code behaves exactly as I would expect: fills elements l1 to l2 of CE with twice the first element of C. I suggest you find a reference book about C and look up double to integer conversion and mixed double/integer arithmetic.
Your Hash is an array of type double with values between 0 and 1. You have j defined as an integer and do:
j=Hash[v2];
it's a double to integer implicit cast. The fractional part of the double is truncated. j is always going to be 0 since the integral part of Hash is always 0.
You also have:
ip = 1/2*(j-i1);
Three problems here:
  • 1/2 is 0. You have two integer literals so you're performing a C integer division. If you want the double 0.5, you need to write 1./2 (dot critical to declare a floating point literal), or just write 0.5
  • Since all the operands on the right hand side are integer, all math is integer math.
  • ip is integer anyway, so even if the right-hand side produced a double value, it would get truncated to its integral part
It's the same everywhere. The end result is that both Cijk and C[ip+(jp)*(K/2+1)] are always C[0] because i1, i2, etc. are always 0.
====
Also, you don't perform any validation of the inputs. Expect your code to segfault (and kill matlab with it) until you do. You never check that nrhs is 9 but merrily dereference prhs. You never check that l1 and l2 are positive integers less than the number of elements in Hash. You never check that the indices you generate from hash are valid indices for C, or that v1 is valid for Node_val.

Weitere Antworten (0)

Kategorien

Mehr zu Debugging and Analysis finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by