Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
passing an array of integers to a mex function

Subject: passing an array of integers to a mex function

From: lena

Date: 5 Jul, 2013 05:18:10

Message: 1 of 2

Hi, I am new to mex files and I am trying to pass an array of integers to a mex file. Here I provide some example of code. I would greatly appreciate if someone would explain why it doesn't work.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "mex.h"
#include <math.h>

void indices_from_array(int pr[]){
    double A[]={0.1,0.2,0.3,0.4,0.5,0.6,0.7};
    int i;
    
    for(i=0;i<3;i++){
        mexPrintf("A[pr[%d]]=%f\n",i,A[pr[i]]);
     }
    return;
}
    
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
/* Declare variables. */
int elements;
int *pr;


 /* Check for proper number of input and output
arguments.
 */
 if (nrhs != 1) {
 mexErrMsgTxt("One input argument required.");
 }


 /* Get the number of elements in the input argument. */
// elements = mxGetNumberOfElements(prhs[0]);

 /* Get the data. */
 pr = (int*)mxGetData(prhs[0]);

// int i;
// for(i=0;i<4;i++){
// mexPrintf("pr[%d]=%d\n",i,pr[i]);
// }
//
 indices_from_array(pr);
return;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
P.S. I understand that an alternative is to pass an array as a double array and then cycle through its elements converting them one by one. But I would like to understand why this method fails.
Thank you in advance, Lena

Subject: passing an array of integers to a mex function

From: James Tursa

Date: 5 Jul, 2013 15:43:15

Message: 2 of 2

"lena" wrote in message <kr5kui$cpo$1@newscl01ah.mathworks.com>...
> Hi, I am new to mex files and I am trying to pass an array of integers to a mex file. Here I provide some example of code. I would greatly appreciate if someone would explain why it doesn't work.
> /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
> #include "mex.h"
> #include <math.h>
>
> void indices_from_array(int pr[]){
> double A[]={0.1,0.2,0.3,0.4,0.5,0.6,0.7};
> int i;
>
> for(i=0;i<3;i++){
> mexPrintf("A[pr[%d]]=%f\n",i,A[pr[i]]);
> }
> return;
> }
>
> void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
> /* Declare variables. */
> int elements;
> int *pr;
>
>
> /* Check for proper number of input and output
> arguments.
> */
> if (nrhs != 1) {
> mexErrMsgTxt("One input argument required.");
> }
>
>
> /* Get the number of elements in the input argument. */
> // elements = mxGetNumberOfElements(prhs[0]);
>
> /* Get the data. */
> pr = (int*)mxGetData(prhs[0]);
>
> // int i;
> // for(i=0;i<4;i++){
> // mexPrintf("pr[%d]=%d\n",i,pr[i]);
> // }
> //
> indices_from_array(pr);
> return;
> }
> ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
> P.S. I understand that an alternative is to pass an array as a double array and then cycle through its elements converting them one by one. But I would like to understand why this method fails.
> Thank you in advance, Lena

Your code relies on the input class matching an int, which is very likely 32-bits on your machine. So add this to your front end checking and see what happens:

if( !mxIsInt32(prhs[0]) ) {
    mexErrMsgTxt("Input argument is not int32 class.");
}

James Tursa

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us