Clear Filters
Clear Filters

Is Calling Python from MATLAB the solution to Fortran MEX compiler compatibility across platforms?

1 view (last 30 days)
Some thoughts,
With the ability of MATLAB calling Python files directly and passing variables from one to the other, is this the route to take instead of building Fortran MEX files? One could compile exisiting Fortran routines into a library to import into Python.
Will this approach impact perfromance in comparison to calling MEX funcitons directly? Anyone using this approach?

Answers (1)

Sanju on 15 Feb 2024
Edited: Sanju on 15 Feb 2024
Hi @Robert,
Using Python to bridge between MATLAB and Fortran can indeed be a practical solution, especially if you have existing Fortran code that you want to integrate into your MATLAB workflow.
Here's how you can implement the approach,
  • You can compile your Fortran code into a shared library ('DLL' on Windows, 'SO' on Unix-like systems) and then use tools like “f2py” or “ctypes” to create a Python module that wraps the Fortran functions.
  • MATLAB has support for calling Python functions directly using the “py” module. This allows you to pass data between MATLAB and Python seamlessly.
  • You can pass data back and forth between MATLAB and Python using various data types such as arrays, scalars, and even more complex data structures like dictionaries or custom classes.
Performance comparisons between using Fortran MEX functions directly and calling them from MATLAB via Python depend on factors like computation nature, Fortran code efficiency, and data passing overhead. While calling external code from MATLAB incurs some overhead, the impact may vary based on your specific use case. Opting for MEX functions is preferable for critical performance and optimized Fortran code. However, using Python with Fortran libraries can be advantageous for flexibility, ease of integration, and diverse tasks such as data manipulation and visualization.
You can also refer to the below documentation if required,
Hope this Helps.


Find more on Fortran with MATLAB in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!