Structure input to Mex file question

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

ssklios Sklios
ssklios Sklios 2011년 11월 14일
Sorry, the code did not copy properly. Here it is:
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;
Fangjun Jiang
Fangjun Jiang 2011년 11월 14일
You can use the {}Code format.
ssklios Sklios
ssklios Sklios 2011년 11월 14일
thank you. Looking back, the code is, in fact, copied correctly in my initial post

댓글을 달려면 로그인하십시오.

 채택된 답변

Kaustubha Govind
Kaustubha Govind 2011년 11월 14일

4 개 추천

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

ssklios Sklios
ssklios Sklios 2011년 11월 14일
Thank you very much for your answer.
What do you mean by this: "so you need to walk through all the fields and assign it to your C type"?
Kaustubha Govind
Kaustubha Govind 2011년 11월 14일
You need to use mxGetField or mxGetFieldByNumber on the input, and loop through all the fields - then you get the mxArray corresponding to the value of each field, and copy the contents of that mxArray to your native C type.
There is no direct way to pass a C structure from MATLAB as far as I know, so you need to do some manual "data marshaling" .
Kaustubha Govind
Kaustubha Govind 2011년 11월 14일
FYI: The only way you can pass in a structure from MATLAB directly into native code is to compile a generic DLL from your C code, and call into that using LOADLIBRARY and CALLLIB. The MATLAB structure can then be automatically mapped to a native type using LIBSTRUCT.
ssklios Sklios
ssklios Sklios 2011년 11월 14일
Thank you again. One last question(hopefully): how do I copy the contents of the mxArray to the C type? I am very unfamiliar with C which is why this is difficult for me.
Kaustubha Govind
Kaustubha Govind 2011년 11월 15일
You can get the underlying C-pointer-type from an mxArray by using mxGetPr/mxGetPi (for double arrays) or mxGetData/mxGetImagData (for all other array types), and then perform either a memcpy (if your C type is also a pointer), or loop through the array and assign it element-by-element. I would recommend looking at the documentation for mxGetPr and mxGetData and look at the examples linked off the bottom of the doc pages.
ssklios Sklios
ssklios Sklios 2011년 11월 15일
Would it be possible to post an example of doing this for a simple structure? I read the documentation, but am still very confused. Thank you.
ssklios Sklios
ssklios Sklios 2011년 11월 20일
please?
James Tursa
James Tursa 2011년 11월 20일
Please show a sample m-code for how you are building the structure on the MATLAB side. Be sure to show the exact field names, etc.
ssklios Sklios
ssklios Sklios 2011년 11월 20일
Thank you for the response. The code below is how the structure is built on the MATLAB side. Please correct me if I'm wrong because my goal is for the input to match the C type poly that I described in my first post.
A=[225 120;353 321;231 262;129 326]; %a sample polygon with x coords in column 1 and y coords in column 2
for i=1:length(A)
point(i).x=A(i,1);
point(i).y=A(i,2);
end
poly.n=length(A);
poly.point=point;
ssklios Sklios
ssklios Sklios 2011년 11월 20일
In this case, does the input even have to be a structure? Or would it be easier for the matlab input to be a matrix and then build the C input structure within the MEX file?
ssklios Sklios
ssklios Sklios 2011년 11월 22일
Any clue? I don't mean to sound impatient but this matter has become urgent
James Tursa
James Tursa 2011년 11월 22일
I will get you an example answer today.
ssklios Sklios
ssklios Sklios 2011년 11월 22일
Thank you. It is much appreciated
ssklios Sklios
ssklios Sklios 2011년 11월 23일
Hello James,
Just a friendly reminder about my mex file input problem.
Thanks again!
ssklios Sklios
ssklios Sklios 2011년 11월 27일
any ideas for how to use this structure as an input for a mexfile?
ssklios Sklios
ssklios Sklios 2011년 11월 29일
please?
Kaustubha Govind
Kaustubha Govind 2011년 11월 29일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by