Returning mxArray* as void* from a C shared library
Mostra commenti meno recenti
For a C function that inputs an mxArray*, one can simply:
calllib(mylib, 'myinputfun', myarray);
If the C function instead inputs a void* (reinterpreted within the function as an mxArray*), then this works:
calllib(mylib, 'myinputfun', libpointer('MATLAB array', myarray));
Similarly, if the C function returns an mxArray*:
myarray = calllib(mylib, 'myoutputfun');
When returning a void* (that actually is an mxArray*), a libpointer with a voidPtr type is returned, which makes sense. However I'm having difficulty accessing the underlying value. I would expect something like this to work, but I cannot seem to cast the pointer to a 'MATLAB array'.
mypointer = calllib(mylib, 'myoutputfun');
setdatatype(mypointer, 'MATLAB array', size);
myarray = mypointer.Value;
I am aware that it would be simpler to work with native types like double*. The reason I require this is because the functions I have are actually creating and returning mxGPUArray*, and I need to preserve the array structure in order preserve its data pointer to GPU memory (hope that makes sense).
Risposta accettata
Più risposte (1)
James Tursa
il 22 Lug 2015
0 voti
Can you create your own custom header file for this library when you load it with loadlibrary, with the (void *) for the function in question changed to (mxArray *) ?
4 Commenti
auneri
il 22 Lug 2015
James Tursa
il 22 Lug 2015
But this is a header for just loadlibrary to use. Why to you need to avoid mex.h?
auneri
il 22 Lug 2015
James Tursa
il 22 Lug 2015
I think maybe you missed the point. You can keep your original header file in pure C, and not change any of your build practices or how you compile the library. This extra header file with (mxArray *) is off to the side, outside of your normal build, and only used for one purpose ... to feed the MATLAB loadlibrary function.
Categorie
Scopri di più su C Shared Library Integration in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!