Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User-defined structs with a MATLAB mex function

Tags:

c

struct

matlab

mex

I'm trying to link my C code to MATLAB through the use of a mex function, and I'm afraid I'm quite confused. Specifically, I want to know whether I can use my own user-defined data types (such as the Person struct in the example below). I haven't come across anything in the documentation to suggest that I can, but it would be unfortunate if this were not allowed! Here's what I have in the way of a gateway function:

void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])  
{   

 int *inA;   
 int *inB;   
 const mxArray *people;   
 const char *fieldnames[5];  
 int numFields, i;  

 inA = (int *) mxGetPr(prhs[0]);  
 inB = (int *) mxGetPr(prhs[1]);  

 numFields = 5;  
 fieldnames[0] = "home";  
 fieldnames[1] = "work";  
 fieldnames[2] = "mode";  
 fieldnames[3] = "loc1";  
 fieldnames[4] = "loc2";  

 people = mxCreateStructMatrix(1000, 1, numFields, fieldnames);  
 for (i = 0; i < numFields; i++)  
 {  
    mxSetField(people,i,fieldnames[i],mxDuplicateArray(prhs[2]));  
 }  

 makePeople(inA, inB, people);  

}

My makePeople function is defined below:

void makePeople(int* A, int* B, Person* people)  
{  

 int MANHATTAN, BRONX, BROOKLYN, QUEENS, STATEN;  

 int i, j, k, p, q, n;  
 int count, home, work, mode;  
 double* loc1;  
 double* loc2;  
 Person oPerson;  

 n = 5;  
 count = 0;  

 MANHATTAN = 1;  
 BRONX = 2;  
 BROOKLYN = 3;  
 QUEENS = 4;  
 STATEN = 5;  

 for (i = 1; i <= n; i++)  
 {  
     for (j = 1; j <= n; j++)  
     {  
          for (k = 1; k <= 2; k++)  
          {  
               if (k == 1)  
                    q = A[n*i+j];  
               else  
                    q = B[n*i+j];  
               for (p = 1; p < q; p++)  
               {  
                    home = i;  
                    work = j;  
                    mode = k;  
                    if (home == MANHATTAN)  
                    {
                          loc1[0] = 2.4;  
                          loc1[1] = 2.4;  
                          loc2[0] = 3.7;  
                          loc2[1] = 3.4;  
                    }  
                    else
                    {  
                          loc1[0] = 3.4;  
                          loc1[1] = 4.4;
                          loc2[0] = 3.7;  
                          loc2[1] = 3.4;  
                    }
                    oPerson = Person_new(home, work, mode, loc1, loc2);  
                    people[count] = oPerson;  
                    count++;  
                    }  
              }  
        }  
  }  
  return;  

}

Finally, here's the person.h file:

 #ifndef PERSON_INCLUDED  
 #define PERSON_INCLUDED  

 typedef struct Person_str *Person;  

 Person Person_new(int home, int work, int mode, double* loc1, double* loc2);  

 #endif  

Any help would be greatly appreciated!

like image 990
arafasse Avatar asked Jan 15 '12 20:01

arafasse


People also ask

Can you pass a structure into a function MATLAB?

You can pass a MATLAB structure to the function and let MATLAB autoconvert the argument. Or you can pass a pointer to a structure, which avoids creating a copy of the structure.

What is MEX function MATLAB?

A MEX file is a function, created in MATLAB, that calls a C/C++ program or a Fortran subroutine. A MEX function behaves just like a MATLAB script or function. To call a MEX function, use the name of the MEX file, without the file extension. The MEX file contains only one function or subroutine.

How use MEX setup in MATLAB?

To change the default, use the mex -setup lang command. MATLAB displays a message with links to select a different default compiler. If you call mex -setup without the lang argument, then MATLAB displays information about the default C compiler. MATLAB also displays links to the other supported languages.

What is MEX C++?

A C++ MEX function is a class that overrides the function call operator, operator() to create a function object or functor. These objects behave like MATLAB® functions that can accept inputs and return outputs.


1 Answers

In MEX, MATLAB structures are of type mxArray, which is nothing like your custom C structure Person. Therefore you cant just assign:

people[count] = oPerson;

// people is defined as: mxArray*
// oPerson is defined as: Person

You will have to fill the MATLAB struct field-by-field, the same way you did in the beginning of your code, through mxSetField

like image 135
Amro Avatar answered Sep 19 '22 04:09

Amro