Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

passing MATLAB structures to C/C++

714 views
Skip to first unread message

Jacob Englander

unread,
Mar 26, 2010, 6:39:04 PM3/26/10
to
I am rewriting a function which was previously implemented in MATLAB as a mex function for speed. The original function took a structure as an input and returned a structure as an output. Is there a way to pass a structure directly to a mex file, or perhaps a way to generate a C/C++ struct from a MATLAB structure?

James Tursa

unread,
Mar 26, 2010, 8:11:03 PM3/26/10
to
"Jacob Englander" <jenglan3....@illinois.edu> wrote in message <hojd27$nnk$1...@fred.mathworks.com>...

> I am rewriting a function which was previously implemented in MATLAB as a mex function for speed. The original function took a structure as an input and returned a structure as an output. Is there a way to pass a structure directly to a mex file, or perhaps a way to generate a C/C++ struct from a MATLAB structure?

Yes. But you have to do it manually piece by piece with the mxCreateStruct, mxGetField, mxSetField, etc. API functions. There is no magic function that will take an arbitrary C/C++ struct and turn it into an mxArray for you.

James Tursa

Máday Péter

unread,
Mar 27, 2010, 4:29:11 PM3/27/10
to
Hello

I was wondering if it should be possible to formulate the problem in
such a way so that the parameters to the mex function would be arrays
and write a wrapper function in MATLAB, that takes care of the
transformation between the representations.

Peter

James Tursa

unread,
Mar 27, 2010, 5:38:05 PM3/27/10
to
Máday Péter <mada...@gmail.com> wrote in message <92e333d9-0be0-4686...@b33g2000yqc.googlegroups.com>...

An m-file wrapper function could get at the individual structure variables and pass them along to the mex routine of course. But that doesn't really save you anything. In fact it just creates a bunch of temporary mxArray header info that you really don't need. The only advantage to this approach IMO is that it may be easier to do some error checking in the m-file vs in a mex routine. Regardless, you will still have to construct your C/C++ struct inside the mex routine ... you can't do it at the m-file level. (Is that what you are asking?). Maybe if you have a specific example of a MATLAB struct and your desired C/C++ struct I could suggest C code that would go back & forth between the two.

James Tursa

Jacob Englander

unread,
Mar 27, 2010, 8:07:04 PM3/27/10
to
Thank you, I appreciate the prompt response. I guess it will be a lot of extra work, but if there is no other way then that's what I'll do. Thanks again.

Philip Borghesani

unread,
Mar 29, 2010, 11:15:57 AM3/29/10
to

"Jacob Englander" <jenglan3....@illinois.edu> wrote in message news:hojd27$nnk$1...@fred.mathworks.com...

>I am rewriting a function which was previously implemented in MATLAB as a mex function for speed. The original function took a
>structure as an input and returned a structure as an output. Is there a way to pass a structure directly to a mex file, or perhaps
>a way to generate a C/C++ struct from a MATLAB structure?
>

If the structures are kept simple with no arrays of pointers than the LOADLIBARY, CALLLIB and LIBSTRUCT commands can be used to pass
a C structure from MATLAB to a c interfaced function.

Take a look at matlabroot\extern\examples\shrlib\shrlibsample.c and the documentation for more information.

Phil


Stefan Griebel

unread,
Jul 14, 2010, 1:28:07 PM7/14/10
to
I'm attempting the same: Passing a matlab structure to C, and unwrapping it using all of the built in mxarray functions. I thought the following example on the Mathworks documentation site would be helpful (http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/f42645.html), but typing it verbatim, I get errors. I'm using R2008a. Does anyone have experience with the libstruct function?

>> sm.p1 = 476; sm.p2 = -299; sm.p3 = 1000
sm =
p1: 476
p2: -299
p3: 1000
>> sc = libstruct('c_struct', sm)
??? Error using ==> feval
Undefined function or method 'lib.c_struct' for input arguments of type 'struct'.

Error in ==> libstruct at 17
ptr=feval(['lib.' structtype],initialvalue);

>>

Ayush Pandey

unread,
Jun 11, 2015, 7:17:46 PM6/11/15
to
"Jacob Englander" <jenglan3....@illinois.edu> wrote in message <hojd27$nnk$1...@fred.mathworks.com>...
> I am rewriting a function which was previously implemented in MATLAB as a mex function for speed. The original function took a structure as an input and returned a structure as an output. Is there a way to pass a structure directly to a mex file, or perhaps a way to generate a C/C++ struct from a MATLAB structure?

I am struggling with a similar problem. Did you find any good way out? Please do help.

Carlos Martinez

unread,
Aug 4, 2015, 2:42:09 PM8/4/15
to
I am having the exact same problem. Was there a solution found for this problem?


"Stefan Griebel" <stefan....@microtune.com> wrote in message <i1ks37$8gu$1...@fred.mathworks.com>...

Karthikeyan Lanka

unread,
Oct 3, 2016, 4:09:09 PM10/3/16
to
"James Tursa" wrote in message <holtrt$6dk$1...@fred.mathworks.com>...
James,

I am working on a similar problem right now. I have a C code (say test.c) which takes a structure variable X containing two fields A and B. A is a vector of 10 values and B has two values. I have these datasets in matlab. In such a situation, can you please suggest a MEX C code that takes inputs from matlab database and passes it to test.c?

I am happy to provide you any other additional information in this regard.

Thank you.

Karthikeyan.

James Tursa

unread,
Oct 5, 2016, 7:53:15 PM10/5/16
to
"Karthikeyan Lanka" wrote in message <nsudsv$nfd$1...@newscl01ah.mathworks.com>...
Here is some very basic code that takes a MATLAB struct as input, converts it into a C struct, performs operations on the C struct, then converts the result back to an mxArray struct for output. This is not production quality code (needs a lot more data validity checks) but hopefully should be enough to get you started.

James Tursa

// Example mex file to copy a MATLAB struct to C, double the values,
// then return the result back to MATLAB
// struct must have field A with 10 doubles, and field B with 2 doubles

#include "mex.h"

typedef struct {
double A[10];
double B[2];
} ABstruct;

void mx2c( ABstruct *, const mxArray * );
void doubleit( ABstruct * );
mxArray *c2mx( ABstruct * );

//------------------------------------------------------------------------

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
ABstruct c;
if( nlhs > 1 ) {
mexErrMsgTxt("Too many outputs");
}
if( nrhs ) {
mx2c( &c , prhs[0] ); // Convert mxArray to C struct
doubleit( &c ); // Double all of the values in the C struct
plhs[0] = c2mx( &c ); // Convert C struct to mxArray
}
}

//------------------------------------------------------------------------

void mx2c( ABstruct *c, const mxArray *mx )
{
mxArray *field;
double *pr;
int i;

if( !mxIsStruct(mx) || mxGetNumberOfElements(mx) == 0 ) {
mexErrMsgTxt("Input must be a non-empty struct");
}
// add other mx checks here
field = mxGetField( mx, 0, "A");
if( field == NULL ) {
mexErrMsgTxt("Input struct must have field A");
}
// add other field checks here
pr = mxGetPr(field);
for( i=0; i<10; i++ ) {
c->A[i] = *pr++;
}
field = mxGetField( mx, 0, "B");
if( field == NULL ) {
mexErrMsgTxt("Input struct must have field B");
}
// add other field checks here
pr = mxGetPr(field);
for( i=0; i<2; i++ ) {
c->B[i] = *pr++;
}
}

//------------------------------------------------------------------------

mxArray *c2mx( ABstruct *c )
{
char *fieldnames[2] = {"A","B"};
mxArray *mx, *field;
double *pr;
int i;

mx = mxCreateStructMatrix( 1, 1, 2, fieldnames );
field = mxCreateDoubleMatrix( 1, 10, mxREAL );
pr = mxGetPr(field);
for( i=0; i<10; i++ ) {
*pr++ = c->A[i];
}
mxSetField( mx, 0, "A", field );
field = mxCreateDoubleMatrix( 1, 2, mxREAL );
pr = mxGetPr(field);
for( i=0; i<2; i++ ) {
*pr++ = c->B[i];
}
mxSetField( mx, 0, "B", field );
return mx;
}

//------------------------------------------------------------------------

void doubleit( ABstruct *c )
{
int i;

for( i=0; i<10; i++ ) {
c->A[i] *= 2.0;
}
for( i=0; i<2; i++ ) {
c->B[i] *= 2.0;
}
}

Karthikeyan Lanka

unread,
Oct 12, 2016, 3:32:13 PM10/12/16
to
This is exactly what I wanted. I was struggling to write something like "c->A[i] = *pr++;" Thank you very much.

Karthikeyan.

"James Tursa" wrote in message <nt43ov$ku5$1...@newscl01ah.mathworks.com>...
0 new messages