Structure input to Mex file question

5 views (last 30 days)
Hello,
I've been reading a lot about about mex files, but I'm still confused on how to pass a structure as an input to a mex file. In particular, the structure has to reflect the following information (this is how it is defined in the C code I am trying to make a mex file for):
typedef double COORD; /* Cartesian coordinate */
typedef struct pt { /* Cartesian point */
COORD x, y;
} POINT;
typedef struct poly { /* polygon of n points */
int n;
POINT pt[MAX_PTS];
} POLY_REC, *POLY;
I would like the input to be the structure poly. How would I go about doing this? Thank you!
  3 Comments
Fangjun Jiang
Fangjun Jiang on 14 Nov 2011
You can use the {}Code format.
ssklios Sklios
ssklios Sklios on 14 Nov 2011
thank you. Looking back, the code is, in fact, copied correctly in my initial post

Sign in to comment.

Accepted Answer

Kaustubha Govind
Kaustubha Govind on 14 Nov 2011
Inputs to MEX-files are simply passed in as if it were a MATLAB function. So you need to construct the same structure type in MATLAB and pass it into MATLAB. However, the struct type is actually passed in as an mxArray, so you need to walk through all the fields and assign it to your C type. Check out the MEX example in $matlabroot/extern/examples/refbook/phonebook.c ($matlabroot is where your MATLAB installation resides), to see how you can access the fields in the structure.
  17 Comments
Kaustubha Govind
Kaustubha Govind on 29 Nov 2011
ssklios: What you're asking for will require us to invest a considerable amount of time, which it doesn't appear anyone is able to spare currently. Could you try going over the MEX examples in the documentation (particularly phonebook.c), and take a shot at it yourself. You can post a new question if you run into specific issues.

Sign in to comment.

More Answers (0)

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!