I am trying to call a function in C++ in Matlab, I thought I have written my functions properly. The function I want to call, looks like this, it has 8 arguments as input.
void LimitedPrice(double &dMarketPosition, double** dmatLimitPrice,
char* FileID, double* dvecOpen, double* dvecClose,
double** dmatTempData, int tick, double dMaxBarBack)
{Bla, Bla, Bla}
I want to get 2 values after running this function. Which stored in dMarketPosition, and dmatLimitPrice since they are pointers.
In this function, I have to use some ** (double pointers) to represent my Matrix.
After running this function, dMarketPosition, dmatLimitPrice will store what I want, since they are pointers. I have tested my function and I believed my function are written properly.
Now I am ready to write my mexFunction(). My mex function somehow looks like this
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs,const mxArray *prhs[])
{
if(nrhs!=8){
mexErrMsgIdAndTxt("LimitPrice: ","I want to 8 inputs");
}
// I want two output
if(nlhs!=2) {
mexErrMsgIdAndTxt("LimitPrice: ","I want to 2 outputs");
}
//The first input is a scalar
double dmarketposition;
dmarketposition = mxGetScalar(prhs[0]);
//The second input is a 2-dimensional Matrix
double **dmatlimitprice;
*dmatlimitprice = mxGetPr(prhs[1]);
/*****Assuming I have checked other inputs here properly*****/
// I want to define my outputs
// Create 1*1 matrix as output, which is the double value I want
plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
//The second output is a two dimensional matrix
plhs[1] = mxCreateDoubleMatrix(2,(mwSize)4,mxREAL);
dmarketposition = mxGetScalar(plhs[0]);
*dmatlimitprice = mxGetPr(plhs[0]);
// Call the function
LimitedPrice(dmarketposition, dmatlimitprice, fileid,
dvecopen, dvecclose, dmattempdata, tick, dmaxbarback);
}
The above function does not seem to work... It seems that in mexFunction(), We would better only pass one-dimensional array, which is double *a like thing.
My key issue is "How can we deal with 2-dimensional array, which is a matrix" in mexFunction().
std::vector<double*>), and fill it with the addresses of each column in the matrix. Then you will have a double pointer to pass to your C++ function.