How to pass an integer array from matlab to mex?

◇◆丶佛笑我妖孽 提交于 2019-12-07 15:44:34

问题


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 

I've seen this answer : using integer arrays on mex but I haven't understood what to do.

Any help would be much appreciated.


回答1:


Here's a table is of equivalent C and MATLAB data types to use when deciding how to pass data.

Source.

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.



来源:https://stackoverflow.com/questions/33426126/how-to-pass-an-integer-array-from-matlab-to-mex

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!