How can I pass a pointer from a MEX function to the MATLAB workspace so I can call it in another MEX function (R2021b)?

6 vues (au cours des 30 derniers jours)
I have a FORTRAN MEX function that calls a C routine which returns a pointer to an allocated data structure. While the pointer is accessible in the MEX function, I would like to know if there is a way to pass the pointer back to MATLAB R2021b so that I can utilize it later on with another M or MEX function. As an example, here is a snippet of how the C function is being called in FORTRAN MEX:
 
INTEGER(KIND=8) myPtr
myPtr = my_C_function(foo)
Now in this FORTRAN MEX, I can access the pointer using '%VAL(myPtr)'. However, I am not sure how to pass 'myPtr' to the MATLAB workspace so that I can call another MEX with 'myPtr' as an input argument through an M function.

Réponse acceptée

MathWorks Support Team
MathWorks Support Team le 24 Juil 2023
To pass a pointer from a FORTRAN MEX function back to MATLAB, you can use the ‘mxCreateNumericMatrix’ function to create a MATLAB array that contains the pointer value as an integer.
Here’s the link to the ‘mxCreateNumericMatrix’ function:
For example, you can set the array type to ‘mxINT64_CLASS’ and utilize the function ‘mxGetInt64s’ to get the pointer value returned by the C function. Here’s the link to this function:
And the next step is to use the ‘mexCallMATLABWithTrap’ function. You can use the MATLAB array you have created with ‘mxCreateNumericMatrix’ as an input argument to another MEX function by passing it as an argument to the ‘mexCallMATLABWithTrap’ function. Here’s the link to the documentation of this function:

Plus de réponses (0)

Catégories

En savoir plus sur Fortran with MATLAB dans Help Center et File Exchange

Tags

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by