Borrar filtros
Borrar filtros

How do I call/use the MATLAB engine from a Fortran program?

3 visualizaciones (últimos 30 días)
Dan W
Dan W el 10 de Jul. de 2020
Comentada: mary el 8 de Nov. de 2022
I'm struggling to get my Fortran code to use the MATLAB engine and run MATLAB scripts/function. I'm using MacOS, gfortran compiler and MATLAB 2019a.
I found an online resource (MATLAB API Guide v5) that describes how to compile the Fortran code:
f77 <include dir> –o <result> <source> <libdir> <libraries>
where:
  • <include dir> is –I<matlabroot>/extern/include
  • <result> is the name of the resulting program
  • <source> is the list of source files
  • <libdir> is –L<matlabroot>/extern/lib/$ARCH (in my case, I'm assuming $ARCH = maci64)
  • <libraries> is –leng –lmx
which does not work for me.
I have added example code samples for MATLAB (saved as testMe.m), Fortran (shortened without error checks and saved as callMat.f90) as well as the complie command:
MATLAB CODE:
a=2;b=3;c=55;d=42;
fprinft("%2.0f + %2.0f = %2.0f", a, c, (a+c));
fprinft("%2.0f x %2.0f = %2.0f", b, d, (b*d));
FORTRAN CODE:
program callMat
#include "engine.h" !not sure if this belongs in/before 'program' or exclude entirely (I've tried all 3 versions).
implicit none
ep = engOpen('matlab ')
engEvalString(ep, 'testMe')
engClose(ep)
end program callMat
GFORTRAN COMPILE COMMAND:
gfortran -I /Applications/MATLAB_R2019a.app/extern/include -o callMat callMat.f90 -L /Applications/MATLAB_R2019a.app/extern/lib/maci64 -leng -lmx
Please could you let me know where I went wrong as I cant even get the example code 'fengdemo.F' working.

Respuesta aceptada

Nipun Katyal
Nipun Katyal el 15 de Jul. de 2020
In order to check if your installation is correct, please follow the steps given in the link below
f77 <include dir> –o <result> <source> <libdir> <libraries>
where:
  • <include dir> is –I<matlabroot>/extern/include
  • <result> is the name of the resulting program
  • <source> is the list of source files
  • <libdir> is –L<matlabroot>/extern/lib/$ARCH (in my case, I'm assuming $ARCH = maci64)
  • <libraries> is –leng –lmx
These steps are for linux os and might not work as you expect them to
since the engine is not present on mac os.
You can compile your fortran file using mex -client engine Filename.F to compile standalone matlab engine applications.
  1 comentario
mary
mary el 8 de Nov. de 2022
I tried to follow the example above to see how a Matlab function can be called in a Fotran code. I am using Linux. I copied "engine.h" from /usr/local/MATLAB/R2021a/extern/include/
I changed the include dir and libdir based on my system. So, I executed the following line in terminal:
gfortran -I /usr/local/MATLAB/R2021a/extern/include -o callMat callMat.f90 -L /usr/local/MATLAB/R2021a/extern/lib/glnxa64 -leng -lmx
And I have following errors:
callMat.f90:5:4:
engEvalString(ep, 'testMe')
1
Error: Unclassifiable statement at (1)
callMat.f90:6:4:
engClose(ep)
1
Error: Unclassifiable statement at (1)
callMat.f90:4:6:
ep = engOpen('matlab ')
1
Error: Symbol epat (1) has no IMPLICIT type
callMat.f90:4:9:
ep = engOpen('matlab ')
1
Error: Function engopenat (1) has no IMPLICIT type
Could you please tell me what should be changed in order to make callMat work?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Fortran with MATLAB en Help Center y File Exchange.

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by