フィルターのクリア

mex : return c struct member value to matlab struct member

5 ビュー (過去 30 日間)
James
James 2012 年 3 月 25 日
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?

回答 (2 件)

James
James 2012 年 3 月 26 日
Hmm not sure why the answer got deleted. Thanks Friedrich, I managed to solve with your suggestion.

Ping
Ping 2013 年 4 月 17 日
I have the same problem to solve. Can you re-post the answer?

カテゴリ

Help Center および File ExchangeWrite C Functions Callable from MATLAB (MEX Files) についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by