필터 지우기
필터 지우기

Interpreting a struct returned by engGetVariable

조회 수: 1 (최근 30일)
Sean McGhee
Sean McGhee 2011년 4월 27일
Hi,
I am adapting matlab code to run from C++ through the matlab engine and I need to retrieve and put a struct which contains scalars, strings and matrices...
How do I navigate through the struct (returned in an mxArray) once I have it and accordingly how do i put info into various sections?
Is it even possible to manipulate a struct variable once returned?
Thanks, Sean

채택된 답변

Chirag Gupta
Chirag Gupta 2011년 4월 27일
You should be able to use mx* struct functions to manipulate and traverse your structure.
bool mxIsStruct(const mxArray *pm)
for example lets you test whether the mxArray is a structure.
Look at the phonebook.c example in MATLABROOT/extern/examples/refbook/
  댓글 수: 1
Sean McGhee
Sean McGhee 2011년 4월 27일
Thanks - I rather expected there would be a way but the blurb in the docs about engGetVariable rather left me hanging...
Looks like your answer will help!
Sean

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

James Tursa
James Tursa 2011년 4월 27일
When you retrieve a variable with engGetVariable you are getting a deep copy of the variable from the engine into your program. You can manipulate it all you want and it will not affect the variable in the engine workspace. In the External Interfaces section of the doc, look at mxIsStruct, mxGetField, mxGetFieldByNumber, mxSetField, mxSetFieldByNumber and friends. If you are changing the contents of a currently existing field, be sure to use mxDestroyArray on the current field first or you will get a memory leak. e.g., do something like this:
mx = engGetVariable(etc);
field = mxGetField(etc);
mxDestroyArray(field);
mxSetField(etc);
Also, be careful not to reuse the same variable to set multiple fields or you will risk a program bomb when the variable gets destroyed. There is a way to do this correctly by bumping up the reference count of the variable, but this technique is not officially supported.
James Tursa
  댓글 수: 1
Sean McGhee
Sean McGhee 2011년 4월 27일
And thanks to you too - this is proving most helpful...:)
And the response times are amazing!

댓글을 달려면 로그인하십시오.


Sean McGhee
Sean McGhee 2011년 4월 28일
I appreciate everyone's help but I am still pulling hen's teeth trying to actually access the data - I might start a new question thread as this one might be closed due to my accepting the answer above (which was quite helpful - no problems there)...
I cannot seem to get data out - i need to see and end to end example of:
1-getting the struct using engGetVariable (already know this part) 2-accessing the desired field (i THINK this works but cannot be sure) 3-actually retrieving the data in the selected field (presumably using mxGetData and some mxCopyTo...routine)
I will look in the other examples in the externals directory but I am doubtful i will find a simple start to finish example of this
Thanks! Sean

카테고리

Help CenterFile Exchange에서 MATLAB Compiler에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by