Search code examples
c++arraysmatlabcompiler-errorsmex

c++ mex error: invalid types ‘double[mwSize]’ for array subscript


I know similar questions have been asked before, but they seem to center around regular c++ code and not that of c++ code written for the creation of mex files.

As the title of this post relates, I get the following error:

  gaussDraw.cpp:110: error: invalid types ‘double[mwSize]’ for array subscript

  mex: compile of ' "gaussDraw.cpp"' failed.

Now, I'm pretty new to coding in c++, but I think I'm picking it up pretty fast.

The following is the declaration of my mexFunction and variable declarations. You will notice I have declared double *x, which appears to be the offending variable (as we will see when I get to line 110):

 void mexFunction(int nlhs, mxArray *plhs[],
    int nrhs, const mxArray *prhs[]){

           int i,j,numactiveid,count;
           mwSize p,n,k;
           double *newX, *stDev, *x, *c, *d, *l, *u, *tol, *y, *finallambda, *finallb, *finalub, *dd;
           int numVals,indexPos,numToDraw,curRandVal;
           double tempXVal,curStd, curMean,lambda,temp,lb,ub,tempDVal,xBarVal;
           int *activeId;

Here's how I'm setting x:

// Get input values
p=mxGetM(prhs[0]); //get the size of the original x vector
x = mxGetPr(prhs[0]);//Original x vector

Here's the offending code block (I've separately declared each value to make it clear where the offense is occurring and have added a comment showing which corresponds to line 110):

for(k=0;k<indexPos;++k){
        tempXVal = tempX[k];
        tempDVal = dd[k];
        xBarVal = x[k];//This is line 110
        temp[k] = xBarVal + (tempXVal *  tempDVal);
}

So, I'm not sure why I'm getting this error. If it matters, x will be a vectory/array passed from matlab to the [hopefully future] mex file. If anybody could help me with this I'd really appreciate it. Thank you!


Solution

  • Your line number appears to be off. It's actually complaining about the line below where temp is a double and you're attempting to index it as temp[k].