编译时 mex mxGetDoubles() 函数的问题

Issues with a mex mxGetDoubles() function when compiling

每当我尝试编译非常简单的测试 mex 函数时,我都会收到错误

"undefined reference to `mxGetDoubles'
collect2.exe: error: ld returned 1 exit status

我使用的是 Matlab R2019a,mingw-w64 6.3.0。他们给出的示例 c 文件 (explore.c) 编译得很好并且还使用了 mxGetDoubles()

我已经尝试使用 mxGetPr(),尽管他们说他们不推荐它并且在文档中说它在 matlab R2018a 之后不应该工作,但它工作得很好。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mex.h"
#include "matrix.h"



void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
    mxDouble *vertices;


    vertices = mxGetDoubles(prhs[1]);//mxGetPr(prhs[1]);

}

事实证明,修复很简单,我不得不使用 mex -R2018a fileName.c 而不是调用 mex fileName.c

但这并没有回答为什么 mex explore.c 即使使用相同的功能也能工作,所以如果有人知道为什么,我会非常感兴趣。我假设它与交错复合体 API 有关,但可能是任何东西。

因为在explore.c中有测试,第686行:

 #if MX_HAS_INTERLEAVED_COMPLEX

澄清答案(假设您只使用实数、双精度变量):

  1. 要么用

    vertices = mxGetDoubles(prhs[1]);
    

    使用命令

    mex -R2018a my_system_function.c
    

    (正如你所建议的)

  2. 或者你也可以使用

    #if MX_HAS_INTERLEAVED_COMPLEX
        vertices = mxGetDoubles(prhs[1]);
    #else
        vertices = mxGetPr(prhs[1]); // pointer to real part
    #endif
    

    使用命令

    mex my_system_function.c
    

    (如 mpre 所暗示的 - 请参阅 "C:\Program Files\MATLAB\R2019b\extern\examples\mex\explore.c" 中的代码)。

在我的例子中,我用它来检索更新函数中的函数参数:

static void mdlUpdate(SimStruct *S, int_T tid)
{
    const real_T   *u    = (const real_T*) ssGetInputPortSignal(S,0);
    real_T         *x    = ssGetRealDiscStates(S);
    const mxArray  *p_mx = (const mxArray *) ssGetSFcnParam(S, 0);

#if MX_HAS_INTERLEAVED_COMPLEX
    const mxDouble *p    = mxGetDoubles(p_mx);
#else
    const double   *p    = mxGetPr(p_mx); // pointer to real part
#endif

   //...

}