Mex file. error in passing data.

4 views (last 30 days)
amit jha
amit jha on 7 Apr 2020
Commented: amit jha on 17 May 2020
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]);
//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)
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.
Node_val(1,:)=[0 0 0];
mex hello_world.c
  1 Comment
amit jha
amit jha on 17 May 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..

Sign in to comment.

Accepted Answer

Guillaume on 7 Apr 2020
Edited: Guillaume on 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:
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.

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!