如何将整数数组从Matlab传递给mex? [英] How to pass an integer array from matlab to mex?
问题描述
我想将整数数组从matlab传递给mex.例如,该数组是a=[1 2 3 4].
我编写了以下代码:
I want to pass an integer array from matlab to mex. The array is for example a=[1 2 3 4].
I wrote the following code:
#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]);
}
当我运行它时,结果是:
When I run it the result is:
a[0]:0 a[1]:1072693248 a[2]:0
我已经看到了这个答案:在mex上使用整数数组,但我没有不知道该怎么办.
I've seen this answer : using integer arrays on mex but I haven't understood what to do.
任何帮助将不胜感激.
推荐答案
在决定如何传递数据时,这里的表具有等效的C和MATLAB数据类型.
Here's a table is of equivalent C and MATLAB data types to use when deciding how to pass data.
由于您正在处理MEX文件中的int *
,因此您应该传递int32
MATLAB数据.但是请注意,C语言中的int
不能保证为32位,但在现代系统中似乎是如此.
Since you are dealing with an int *
in the MEX file, you should be passing int32
MATLAB data. Note however that int
in C is not guaranteed to be 32-bit, but seems to be so on modern systems.
这篇关于如何将整数数组从Matlab传递给mex?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!