MATLAB: Linking Fortran to Matlab-generated MEX files

codegencompilefortranmatlab codermex

Hey!
I have written a trajectory code in Matlab and sped it up by compiling it into C using the codegen command. I run this code from matlab as a Trajectory_Mex function and got a 100x speed up compared to the original Matlab script, which is great given that even with the speed up my calculations take a few hours on a cluster.
The trajectory calculations need potential functions, which were provided to me in a Fortran format. After going through Matlab documentation I learned that I could also make Fortran-mex files, that work from the main Matlab workspace but when I try to compile a Matlab script that uses the Fortran-mex file I get the " ??? Only MATLAB files are supported for code generation. Unsupported file extension 'mexa64' for '/PotentialTests/O3PES/timestwo.mexa64'" error.
Is it possible to use a Fortran-mex within a codegen-compiled script? Do I have to rewrite the Fortran files to use codegen? What is the best way for me to implement my Fortran code within compiled scripts?
Thank you very much! MK.

Best Answer

If your goal is to generate a MEX function, then I would recommend just declaring your Fortran MEX function as an extrinsic function:
function y = extrinsicTimesTwo(x)
coder.extrinsic('timestwo');
y = 0;
y = timestwo(x);
>> codegen extrinsicTimesTwo -args 1
This will allow the call to the Fortran MEX function to occur similar to how you call it in MATLAB.
See:
for the doc.