Effacer les filtres
Effacer les filtres

mex : return c struct member value to matlab struct member

3 vues (au cours des 30 derniers jours)
James
James le 25 Mar 2012
I understand that in order to pass the matlab struct(and return), these are the steps to follow:
1)Do an iteration through matlab struct to copy to my C struct.
2)Make a copy of the matlab struct (for llhs, since we cant change anything of prhs)
3)run the c subroutine
4)after running the c subroutine, the changes made to the C struct members are copied to the new matlab struct(created in step2)
I have done steps 1-3 using the phonebook example, but I have no idea how to copy the value from the modified C struct back to the matlab struct. I have tried the function below but it does not make changes:
Let's say my C struct is as below:
typedef struct{ double a; double b; }mycstruct_type
my mex function: void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) {
..copy prhs matlab struct to c struct
..make a copy of prhs to plhs[0]
/*this section below try to make changes in c struct to matlab struct */
mxArray *mat_a = mxCreateDoubleMatrix(1, 1, mxREAL);
double *ptr;
ptr=mxGetPr(mat_a);
ptr=&mycstruct.a;
dummyCfunction(&mycstruct);
mxSetField(plhs[0],0,"a",mat_a);
return
}
the dummyCfunction will make changes to mycstruct.a and I want this changes to be reflected back at the matlab struct field (a) in plhs[0]. How do I do that?

Réponses (2)

James
James le 26 Mar 2012
Hmm not sure why the answer got deleted. Thanks Friedrich, I managed to solve with your suggestion.

Ping
Ping le 17 Avr 2013
I have the same problem to solve. Can you re-post the answer?

Catégories

En savoir plus sur Write C Functions Callable from MATLAB (MEX Files) dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by