Hi,
I am trying to run a part of my matlab code in c++.
Following this example: https://au.mathworks.com/help/matlab/matlab_external/handling-complex-data-c.html
I am working with Matlab2018a, downloaded and installed mingw-w64 c/c++ compiler from the add on menu. i did set up by
mex -setupmex -setup cpp
After number of corrections, I have this code:
#include "mex.h"#include <iostream>#include <stdlib.h>#include <complex>using namespace std;/* computational subroutine */void myMexFunction(const mxArray * lambda,const mxArray * ll_n, mxArray * output, int N){ mwSignedIndex n,s,r; mwSignedIndex M = N/2-1; mwSignedIndex M1 = M+1; mwSignedIndex c,d,rmin,rmax; mexPrintf("M%d\n", M); /* get pointers to the arrays */ mxDouble * lam = mxGetDoubles(lambda); mxComplexDouble * lln = mxGetComplexDoubles(ll_n); mxComplexDouble * out = mxGetComplexDoubles(output); mexPrintf("inside the function\n"); /* perform the nestedloop op */ for(n = -M; n < M; ++n){ mexPrintf("inside the first loop\n"); c = min(M-n,M); mexPrintf("c %d\n", c); d = max(-M,-M-n); mexPrintf("d %d\n", d); for(s = -M; s < M; ++s){ mexPrintf("inside the second loop\n"); rmin=max(s-M,d); rmax=min(s+M,c); mexPrintf("rmin %d\n", rmin); mexPrintf("rmax %d\n", rmax); for(r= rmin; r<rmax; ++r){ mexPrintf("inside the third loop\n"); out[1].real=1; out[2].imag=1; // out[N*(s+M)+(n+M1)].real = // out[N*(s+M)+(n+M1)].real + (lln[s-r+M1].real * lln[n+r+M1].real - lln[s-r+M1].imag * lln[n+r+M1].imag)*lam[r+M1]; // out[N*(s+M)+(n+M1)].imag = // out[N*(s+M)+(n+M1)].imag + (lln[s-r+M1].real * lln[n+r+M1].imag + lln[s-r+M1].imag * lln[n+r+M1].real)*lam[r+M1]; } } }}/* The gateway routine. */void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ){ int N = (int) mxGetScalar(prhs[3]);; /* coppy array and set the output pointer to it */ plhs[0] = mxDuplicateArray(prhs[2]); /* call the C subroutine */ myMexFunction(prhs[0], prhs[1], plhs[0], N); return; }
When I call this function from matlab using:
mex myMexFunction.cpp -R2018aLsn_=myMexFunction(lambda,ll_n,Lsn,N);
I can see that the function goes into all loops but if I assign any value to out[] matlab crashes. I cannot assign even a fixed value to out[1] so it is not index related.
What can i do to get it work?
Thanks
Best Answer