Calling a Matlab function in Fortran using mex tool
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi!
Using the MATLAB Engine API for Fortran, I am trying to call a simple Matlab function from a Fortran code.
I followed the fengdemo example found here : https://fr.mathworks.com/help/matlab/matlab_external/build-and-run-fortran-engine-applications-on-linux.html . It worked, so I want to adapt my Fortran code to call a specific Matlab script I wrote.
My Matlab script call_fortran.m is very simple: it takes x as an entry and multiplies it by 2:
function multiply = call_fortran(x)
multiply = 2*x;
end
I want my Fortran code to generate a variable my_x, open a Matlab session, send the variable to the workspace, apply the function call_fortran and display the result. Using the fengdemo.f code, I wrote
program main
C Declarations
implicit none
mwPointer engOpen, engGetVariable, mxCreateDoubleMatrix
mwPointer mxGetPr
mwPointer ep, my_x ! ep: variable linked to engOpen, starting a Matlab session, my_x: variable que je veux donner a Matlab
double precision my_x
integer engPutVariable, engEvalString, engClose
integer temp, status
mwSize i
my_x = 6
ep = engOpen('matlab ')
if (ep .eq. 0) then
write(6,*) 'Can''t start MATLAB engine'
stop
endif
C Place the variable my_x into the MATLAB workspace
status = engPutVariable(ep, 'my_x', my_x)
C
if (status .ne. 0) then
write(6,*) 'engPutVariable failed'
stop
endif
! My issue now is to call the correct Matlab script
C nlhs = 1
C plhs = 1
C nrhs = 1
C prhs = 1
C integer*4 mexCallMATLAB(nlhs, plhs, nrhs, prhs, functionName)
So I have my my_x, I send it to Matlab, but how do I apply the call_fortran.m function and get the new value of my_x ?
Thank you in advance for your help!
2 commentaires
Réponses (0)
Voir également
Catégories
En savoir plus sur MATLAB Compiler dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!