mex : return c struct member value to matlab struct member
1 view (last 30 days)
Show older comments
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?
0 Comments
Answers (2)
See Also
Categories
Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!