MATLAB: How to run the first Mex function

mexmingw-w64 c/c++ compilerpath

Hi,
I am trying to run a part of my matlab code in c++.
I am working with Matlab2018a, downloaded and installed mingw-w64 c/c++ compiler from the add on menu. i did set up by
mex -setup
mex -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 -R2018a
Lsn_=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

Some problems:
1)
/* coppy array and set the output pointer to it */
plhs[0] = prhs[2];
The above line does not copy the input array to the output array. It only copies a pointer to a pointer. Furthermore, you cannot copy input prhs[] pointers to output plhs[] pointers directly. This violates the rules and can cause a MATLAB crash. Probably what you want instead is a deep copy:
plhs[0] = mxDuplicateArray(prhs[2]);
2)
out[s+M1,n+M1].real = out[s+M1,n+M1].real + etc.
The above line does not do the indexing like you think it is doing. This is not the way to index into a 2D array in C/C++. What you are actually doing in the above line is evaluating the result of a comma operator and using that as a single index. Since the result of a comma operator is the value of the 2nd expression, the above line is the equivalent of
out[n+M1].real = out[n+M1].real + etc.
You will need to manually calculate the equivalent linear index corresponding to the 2D element you are trying to update, and use that linear index instead of what you are doing.
3)
int N = prhs[3];
The above line copies an mxArray * type into an integer. This does not get the value into N. To do that you need to do this instead:
int N = (int) mxGetScalar(prhs[3]);
There are probably other errors too, but I can't read your screen shot above. Please delete the screen shot and post the error messages as text instead.