Problems re-transferring variables from Mex File to Matlab

1 view (last 30 days)
I'm trying to build a mex file from a specific function in C code called quadknap.c (<http://www.diku.dk/~pisinger/quadknap.c)>. My aim is to define the variables no (= number of items), cap (=capacity), ptab (=profit matrix) and wtab (=weight matrix)in Matlab and to run the mex file with these variables as inputs.
As a result I need to get back the variables z (=value of the optimal knapsack) and xtab (=0-1 soluction vector) as outputs to work with the variables in Matlab.
The original code has no main function. Thus, I have integrated the mexFunction and included mex.h and matrix.h. The mexFunction looks like this
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
double *ptab, *wtab, *xtab;
double no, cap, z;
no = mxGetScalar(prhs[0]);
cap = mxGetScalar(prhs[1]);
ptab = mxGetPr(prhs[2]);
wtab = mxGetPr(prhs[3]);
plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
plhs[1] = mxCreateDoubleMatrix(1, no, mxREAL);
z = mxGetScalar(plhs[0]);
xtab = mxGetPr(plhs[1]);
quadknap(no, cap, ptab, wtab, x);
mexCallMATLAB(0, NULL, 1, &prhs[2], "disp");
return;
}
My problem is that I got back only empty arrays plhs[0] and plhs [1] in Matlab then I run the mex file. For associating pointers and arrays I followed the xtimesy.c example of the Matlab extern examples. However, it seems like there is an error in this association for the output variables I do not find.
Kind regards, Matthias

Accepted Answer

James Tursa
James Tursa on 22 May 2015
Edited: James Tursa on 22 May 2015
I doubt you are getting back "empty" arrays. I think what you meant was you are getting back arrays with only 0's in them.
The reason is that you never set the output plhs[0] or plhs[1] data areas to anything after creating them. You need to get the pointers to the data areas and then use them to put values in those areas. E.g.,
double *ptab, *wtab, *xtab, *pr;
:
pr = mxGetPr(plhs[0]);
xtab = mxGetPr(plhs[1]);
*pr = quadknap(no, cap, ptab, wtab, xtab); // Did you mean xtab as last argument?
I assumed that you meant to type xtab as the last argument, but I haven't looked over the quadknap code to see if that is an output so you will need to check this yourself. I also assumed that the return value of quadknap is the "z" you are trying to return as the 1st output.

More Answers (1)

Matthias
Matthias on 25 May 2015
You are right. I'm getting back arrays with only 0's in them.
You are also irght that "z" ist the return value of the quadknap function. The quadknap function in the code is defined that way:
quadknap(int no, int cap, int *ptab, int *wtab, int *xtab)
and then xtab is occuring only again here (within the quadknap function):
/* copy solution vector to xtab */
memcpy(xtab, xstar, sizeof(int) * MSIZE);
Thus, I'm not really sure aboutxtab, if it really is treated as an output variable in the function. But I have interpreted as the solution vector and as output variable of the mexFunction. The goal is to get the solution vector xstar and work with it in Matlab. If there would be another way to get it, I'm also fine.
With your comment on the mexFunction, it has now the following form
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
double *ptab, *wtab, *xtab, *pr;
double no, cap;
no = mxGetScalar(prhs[0]);
cap = mxGetScalar(prhs[1]);
ptab = mxGetPr(prhs[2]);
wtab = mxGetPr(prhs[3]);
plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
plhs[1] = mxCreateDoubleMatrix(1, no, mxREAL);
pr = mxGetPr(plhs[0]);
xtab = mxGetPr(plhs[1]);
*pr = quadknap(no, cap, ptab, wtab, xtab);
return;
}
Compiling works without problems. However, Matlab crashes when running the respective mex file. It seems that it tries to calculate and then crashes.
I've attached a txt file with the error message. IS there a possibility to say if the error is in the mexFunction or in the original code?
Thank you and kind regards, Matthias

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!