【发布时间】:2015-01-15 20:02:47
【问题描述】:
我一直在尝试使用 Matlab 编译器创建一个 C 共享库,一段时间以来,它将用作不同应用程序的插件库。我最近以为我已经完成了这项任务,只是意识到我从新的“Matlab 编译”共享库中调用的函数需要将其返回转换为 C 结构。
我使用 Matlab Answers 网站上的示例来帮助我创建包装器 level2 函数来调用需要返回结构的 Matlab 函数。(http://www.mathworks.com/matlabcentral/answers/94715-how-do-i-wrap-matlab-compiler-4-8-r2008a-created-c-dlls-to-create-another-dll)
我的问题出在下面代码的 Convert 返回的 MATLAB 数据到 C 数据 部分。我可以很好地转换为整数、双精度、字符等,但我无法弄清楚如何编写从 matlab 返回的 mxArray 到 C 结构的转换。
/* Wrapper for level 1 function exported by the MATLAB generated DLL *
* This function converts C data to MATLAB data, calls the MATLAB generated *
* function in level1.dll and then converts the MATLAB data back into C data */
int wmlfLevel1(double* input2D, int size, char* message, double** output2d){
int nargout=1;
/* Pointers to MATLAB data */
mxArray *msg;
mxArray *in2d;
mxArray *out2d=NULL;
/* Start MCR, load library if not done already */
int returnval=isMCRrunning();
if(!returnval)
return returnval;
/* Convert C data to MATLAB data */
/* IMPORTANT: this has to be done after ensuring that the MCR is running */
msg=mxCreateString(message);
in2d=mxCreateDoubleMatrix(size, size, mxREAL);
memcpy(mxGetPr(in2d), input2D, size*size*sizeof(double));
/* Call the M function */
returnval=mlfLevel1(nargout, &out2d, in2d, msg);
/*Convert returned MATLAB data to C data */
*output2d=(double *)malloc(sizeof(double)*size*size);
memcpy(*output2d, mxGetPr(out2d), size*size*sizeof(double));
/* Clean up MATLAB variables */
mxDestroyArray(msg);
mxDestroyArray(in2d);
mxDestroyArray(out2d);
return returnval;
}
到目前为止,我已尝试使用 mxCreateStructMatrix 函数,我尝试创建 C 结构骨架,我即将尝试 libstruct 函数,但由于我是 C 编程和 Matlab 编译器的新手,任何帮助将不胜感激!
【问题讨论】:
标签: c matlab structure matlab-deployment matlab-compiler