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
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
2011 年 11 月 14 日
You can use the {}Code format.
ssklios Sklios
2011 年 11 月 14 日
thank you. Looking back, the code is, in fact, copied correctly in my initial post
採用された回答
Kaustubha Govind
2011 年 11 月 14 日
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
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
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
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
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
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
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
2011 年 11 月 20 日
please?
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
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
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
2011 年 11 月 22 日
Any clue? I don't mean to sound impatient but this matter has become urgent
James Tursa
2011 年 11 月 22 日
I will get you an example answer today.
ssklios Sklios
2011 年 11 月 22 日
Thank you. It is much appreciated
ssklios Sklios
2011 年 11 月 23 日
Hello James,
Just a friendly reminder about my mex file input problem.
Thanks again!
ssklios Sklios
2011 年 11 月 27 日
any ideas for how to use this structure as an input for a mexfile?
ssklios Sklios
2011 年 11 月 29 日
please?
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) についてさらに検索
参考
2011 年 11 月 14 日
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
