如何将整数数组从 matlab 传递到 mex?
How to pass an integer array from matlab to mex?
我想将一个整数数组从 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但我还不知道该怎么做。
如有任何帮助,我们将不胜感激。
这里有一个 table 在决定如何传递数据时要使用的等效 C 和 MATLAB 数据类型。
由于您正在处理 MEX 文件中的 int *
,因此您应该传递 int32
MATLAB 数据。但是请注意,C 中的 int
不保证是 32 位的,但在现代系统上似乎是这样。
我想将一个整数数组从 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但我还不知道该怎么做。
如有任何帮助,我们将不胜感激。
这里有一个 table 在决定如何传递数据时要使用的等效 C 和 MATLAB 数据类型。
由于您正在处理 MEX 文件中的 int *
,因此您应该传递 int32
MATLAB 数据。但是请注意,C 中的 int
不保证是 32 位的,但在现代系统上似乎是这样。