如何从Matlab传递整数数组到Mex?

3

我希望能够将一个整数数组从Matlab传递到Mex。例如,这个数组是a=[1 2 3 4]。我编写了以下代码:

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <mkl.h>
#include "mkl_vml.h"
#include "mex.h"
#include "matrix.h"
#include "mkl_vsl.h"


/* main fucntion */
void mexFunction(int nlhs,  mxArray *plhs[], int nrhs, const mxArray *prhs[])
{

    int n, *a;

    /* make pointers to input data */
    n = (int)mxGetScalar(prhs[0]);
    a = (int *)mxGetData(prhs[1]);

    mexPrintf("a[0]:%d \t a[1]:%d \t a[2]:%d \n", a[0],a[1],a[2]);

}

当我运行它时,结果是:
a[0]:0   a[1]:1072693248     a[2]:0 

我看到了这个答案:using integer arrays on mex,但我并不明白该做什么。
如果能得到帮助,将不胜感激。

1个回答

1
这里有一张表格,列出了在决定如何传递数据时使用的等效C和MATLAB数据类型。

enter image description here

源代码

由于您在MEX文件中处理的是一个int *,因此应传递int32 MATLAB数据。但值得注意的是,在C中,int不能保证为32位,但在现代系统上似乎是这样。


谢谢提供这张表格,非常有用。 - F.F.
@F.F. 不用谢。具体来说,匹配 C 中的 int 的 MATLAB 类型可能是 int32 - chappjc
是的,它有所帮助。感谢您和@Ramashalanka。对不起,我忘记接受它了。 - F.F.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接