Interpreting a struct returned by engGetVariable
Ältere Kommentare anzeigen
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
Akzeptierte Antwort
Weitere Antworten (2)
James Tursa
am 27 Apr. 2011
0 Stimmen
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 Kommentar
Sean McGhee
am 27 Apr. 2011
Sean McGhee
am 28 Apr. 2011
0 Stimmen
Kategorien
Mehr zu Call MATLAB from C finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!