I am trying to pass as argument of a mexfunction, an integer representing the number of columns for mxCreateDoubleMatrix. This integer is not supposed to be used anywhere else than in the main mexFunction.
Somehow, this doesn't seem to work.
// mex function for calling c++ code .
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
char *input_buf;
size_t buflen;
double ncols;
double *result_final;
double *result_second;
/* get the length of the input string */
buflen = (mxGetM(prhs[0]) * mxGetN(prhs[0])) + 1;
/* copy the string data from prhs[0] into a C string input_ buf. */
input_buf = mxArrayToString(prhs[0]);
/* copy the int from prhs[0] to decide on length of the results. */
ncols = (int) (size_t) mxGetPr(prhs[1]);
plhs[0] = mxCreateDoubleMatrix(1, ncols, mxREAL);
plhs[1] = mxCreateDoubleMatrix(1, ncols, mxREAL);
result_final = mxGetPr(plhs[0]);
result_second = mxGetPr(plhs[1]);
/* Do the actual computations in a subroutine */
subroutine(input_buf, buflen, result_final, result_second);
}
If I take out the ncols line, all the rest works as expected. I did not include ncols as input to subroutine because it is not actually used there but only in the main, to define the size of the output array.
I am expected to have a matrix of 1x100 for the output array if I call myfun('examplefile.txt',100) instead the matrix that is shown at the end of the call has an infinite/very long number of columns.
Someone can help with this?
prhs[0]andprhs[1]without knowing if they exist. You should always test for input variables first. If you accidentally call your MEX-function with too few input arguments, your MATLAB will crash, instead of just giving an error message. - Cris Luengo