How to call mxArray with varargin in C; mxCreateCellMatrix and mxSetCell?

4 次查看(过去 30 天)
Compile/build works, but I only get the first argument into matlab function and then program (.exe) crashes. Can I use the mxCreateCellMatrix as input to varargin?
int wmlfNor(char* filenameinp, char* filenameana, char* filenamecsv){
int nargout=1;
mxArray *pfinp;
mxArray *pfana;
mxArray *pfcsv;
mxArray *pvarargin;
mxArray *out=NULL;
int returnval=isMCRrunning();
if(!returnval)
return returnval;
pfinp=mxCreateString(filenameinp);
pfana=mxCreateString(filenameana);
pfcsv=mxCreateString(filenamecsv);
pvarargin=mxCreateCellMatrix(3,1);
mxSetCell(pvarargin,0,pfinp);
mxSetCell(pvarargin,1,pfana);
mxSetCell(pvarargin,2,pfcsv);
%Call to my Matlab function
returnval=mlfNor(nargout,&out, pvarargin);
mxDestroyArray(pfinp);
mxDestroyArray(pfana);
mxDestroyArray(pfcsv);
mxDestroyArray(pvarargin);
mxDestroyArray(out);
return returnval;
}

采纳的回答

Geoff Hayes
Geoff Hayes 2014-10-17
Oyvind- it isn't clear how you are executing the above code. Is it part of a C program that you are running? Where is the function that you are providing the variable number of inputs to? How does wmlfNor relate to this function, as you seem to be just passing in strings here?
If I take the body of your above code and paste it into a MEX function (commenting out the returnval stuff and the call to the mlfNor function), the build/compile works fine, but crashes as soon as I call the MEX function. (Note that I created dummy strings in place of your input parameters.)
What seems to be happening is that the code is trying to destroy/free the same allocated memory twice at
mxDestroyArray(pfinp);
mxDestroyArray(pfana);
mxDestroyArray(pfcsv);
mxDestroyArray(pvarargin);
We destroy the mxArrays that correspond to the three strings created with mxCreateString, and then try to destroy the cell array, pvarargin, which tries to destroy/free all cells...which includes the three (string) mxArrays which we just destroyed. If you comment out these three lines and just do
//mxDestroyArray(pfinp);
//mxDestroyArray(pfana);
//mxDestroyArray(pfcsv);
mxDestroyArray(pvarargin);
it works fine: there is no crash when running the MEX function. To see the effects of this more clearly, do the following: free/destroy the memory to the three string arrays, and then set each of the three cells in pvarargin to NULL, then destroy the cell array
mxDestroyArray(pfinp);
mxDestroyArray(pfana);
mxDestroyArray(pfcsv);
mxSetCell(pvarargin,0,NULL);
mxSetCell(pvarargin,1,NULL);
mxSetCell(pvarargin,2,NULL);
mxDestroyArray(pvarargin);
Again, the function can be built/compiled without any problems, and the MEX function can be run.
Try using one of the above two options in your code, and see if that prevents the crash.
  1 个评论
Oyvind
Oyvind 2014-10-20
It works, thank you. I had another error in the code, should be
pvarargin=mxCreateCellMatrix(1,3);
The code is a layer/library (.dll) between C-code the test engineers write (.exe) and my matlab function mlfNor.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 C Matrix API 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by