Passing a vector object to Matlab trough Mex routine
Afficher commentaires plus anciens
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 ?
Réponses (2)
Geoff
le 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 commentaire
Geoff
le 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
le 20 Mar 2012
0 votes
3 commentaires
Geoff
le 20 Mar 2012
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
le 21 Mar 2012
James Tursa
le 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
Catégories
En savoir plus sur Deploy to C++ Applications Using MATLAB Data API (C++11) dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!