简体   繁体   中英

How to pass an integer array from matlab to mex?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM