Passing a vector object to Matlab trough Mex routine
7 views (last 30 days)
Show older comments
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 ?
0 Comments
Answers (2)
Geoff
on 20 Mar 2012
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 Comment
Geoff
on 20 Mar 2012
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
on 20 Mar 2012
3 Comments
James Tursa
on 21 Mar 2012
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
See Also
Categories
Find more on Deploy to C++ Applications Using MATLAB Data API (C++11) in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!