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 个评论
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;
You can use the {}Code format.
thank you. Looking back, the code is, in fact, copied correctly in my initial post
采纳的回答
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 个评论
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"?
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" .
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.
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.
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.
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.
please?
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.
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;
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?
Any clue? I don't mean to sound impatient but this matter has become urgent
I will get you an example answer today.
Thank you. It is much appreciated
Hello James,
Just a friendly reminder about my mex file input problem.
Thanks again!
any ideas for how to use this structure as an input for a mexfile?
please?
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) 的更多信息
另请参阅
2011-11-14
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
