Problems re-transferring variables from Mex File to Matlab
Mostrar comentarios más antiguos
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
Respuesta aceptada
Más respuestas (1)
Matthias
el 25 de Mayo de 2015
Categorías
Más información sobre Write C Functions Callable from MATLAB (MEX Files) en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!