Passing a vector object to Matlab trough Mex routine

I have an definition of a vector as typedef std::vector<Ipoint> IpVec;
Ipvec ipts; //this is the object i Use.
Ipoint is a class which has the following variables:
float x, y;
float scale;
float orientation;
int laplacian;
float descriptor[64];
float dx, dy;
int clusterIndex;
How do I return ipts back to Matlab ?

回答 (2 件)

Geoff
Geoff 2012 年 3 月 20 日

0 投票

You need to build the struct yourself. This example might help point you in the right direction:
If you don't have the code, I found an old version here:
While I haven't tried this, and I'm not sitting in front of MatLab or a C++ compiler right now, I don't expect it would be a problem.
Try something like this to start:
const int nfields = 9;
const char * fieldnames[] = {
"x", "y",
"scale",
"orientation",
"laplacian",
"descriptor",
"dx", "dy",
"clusterIndex"
};
plhs[0] = mxCreateStructMatrix( ipts.size(), 1, nfields, fieldnames );
for( int i = 0; i < ipts.size(); i++ ) {
lpoint & p = ipts[i];
mxSetFieldByNumber(plhs[0], i, 0, mxCreateDoubleScalar(p.x));
mxSetFieldByNumber(plhs[0], i, 1, mxCreateDoubleScalar(p.y));
mxSetFieldByNumber(plhs[0], i, 2, mxCreateDoubleScalar(p.scale));
// etc...
}
You may want to make a helper functions for creating and copying the 64-elem descriptor array, as well as something to explicitly create integer scalars if you don't want them to be converted to doubles.

1 件のコメント

Geoff
Geoff 2012 年 3 月 20 日
Oh yes, if that doesn't work you can take the approach from the phonebook example, which creates a 1x1 struct matrix and then creates an array (or a matrix in the case of the descriptor field) for each field. That would probably be more efficient, actually.

サインインしてコメントする。

Rohit
Rohit 2012 年 3 月 20 日

0 投票

I am sorry I wasnt able to follow you. the following error comes : error C2440: 'initializing' : cannot convert from 'Ipoints' to 'ipts &' error C2039: 'x' : is not a member of 'std::vector<_Ty>' with [ _Ty=Ipoint ] main1.cpp(890) : error C2039: 'y' : is not a member of 'std::vector<_Ty>' with [ _Ty=Ipoint ] main1.cpp(891) : error C2039: 'scale' : is not a member of 'std::vector<_Ty>' with [ _Ty=Ipoint ] main1.cpp(892) : error C2039: 'orientation' : is not a member of 'std::vector<_Ty>' with [ _Ty=Ipoint ] main1.cpp(893) : error C2039: 'laplacian' : is not a member of 'std::vector<_Ty>' with [ _Ty=Ipoint ] main1.cpp(894) : error C2039: 'descriptor' : is not a member of 'std::vector<_Ty>' with [ _Ty=Ipoint ] main1.cpp(895) : error C2039: 'dx' : is not a member of 'std::vector<_Ty>' with [ _Ty=Ipoint ] main1.cpp(896) : error C2039: 'dy' : is not a member of 'std::vector<_Ty>' with [ _Ty=Ipoint ] main1.cpp(897) : error C2039: 'clusterIndex' : is not a member of 'std::vector<_Ty>' with [ _Ty=Ipoint ]
Also I wasnt able to understand creating helper functions. i am pretty new to mex.

3 件のコメント

Geoff
Geoff 2012 年 3 月 20 日
Oh, yes my code should say 'lpoints', not 'lpVec'. I'll edit the original. I assume that while you are new to MEX, you are familiar with C++ and the STL? Do bear in mind I wrote this snippet without sitting in front of MatLab or a compiler, and I haven't used mex's structs before. So take my answer as a reasonable starting point, but also with a grain of salt! =)
Rohit
Rohit 2012 年 3 月 21 日
I tried it, but the following error comes.
error C2664: 'mxCreateDoubleScalar' : cannot convert parameter 1 from 'float [64]' to 'double'
There is no context in which this conversion is possible
If i remove that line then these errors come
main1.obj : error LNK2019: unresolved external symbol _mxSetFieldByNumber_700 referenced in function _mexFunction
main1.obj : error LNK2019: unresolved external symbol _mxCreateDoubleScalar referenced in function _mexFunction
main1.obj : error LNK2019: unresolved external symbol _mxCreateStructMatrix_700 referenced in function _mexFunction
main1.mexw32 : fatal error LNK1120: 3 unresolved externals
James Tursa
James Tursa 2012 年 3 月 21 日
Can you post the code you currently have for us to look at? The error C2664 above sounds like you passed a float array into the routine instead of a float

サインインしてコメントする。

カテゴリ

製品

タグ

質問済み:

2012 年 3 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by