Problems re-transferring variables from Mex File to Matlab
Show older comments
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
More Answers (1)
Matthias
on 25 May 2015
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!