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 passingint32
MATLAB data. Note however thatint
in C is not guaranteed to be 32-bit, but seems to be so on modern systems.