필터 지우기
필터 지우기

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 CenterFile Exchange에서 Write 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