- Write a single gateway routine in a separate file that contains the subroutine mexFunction(...)
- In the mexFunction(...) routine, extract the inputs and pass them to your existing Fortran code
- Modify your existing Fortran code to accept the inputs from the mexFunction(...) subroutine
- Use the mex command to compile your code, listing the gateway file first and all your other files last
How do i compile multiple fortran code from matlab command
4 views (last 30 days)
i have legacy codes written in fortran 77 (code is classified, can't share) that i need to run/compile from matlab command. (for looping/calibration purposes)
this fortran codes comprised of 1 main routine .FOR and multiple separated .FOR script containing subroutines (see below). the output is a txt files. this main routine call all those subroutines located in different .FOR file.
below is the command i usually used to compile those code (generating executable called output.exe)
from what i read you can use MEX file, but i am having a hard time to understand the documentation since i dont have many experience in CS stuff (looks very complicated). when i look at the practical example, the mex file is only used to run a single subroutine that later will be called as function in matlab, but surely hard to implement in my case. (i dont need to access the output directly since i can read txt file from matlab)
how do i compile these multi-script program just from matlab.
thank you for whoever considering want to help me
James Tursa on 10 Jun 2022
Edited: James Tursa on 10 Jun 2022
To pass variables from MATLAB to your Fortran code you would typically do the following:
Note that other than the interface changes, no other modifications are needed to your existing code. This assumes your code doesn't have any console I/O such as read(5,... or write(6,...
There are examples of these steps in the doc and in the FEX. If you need help with any of the steps let us know. What variables will you be passing to your Fortran code? E.g., what is the class and size and complexity of the variables? If the variables are small enough then deep copying them to pass into your Fortran code will be acceptable (this is what the official examples do). If they are huge then we may want to discuss methods to pass pointers or references to those variables to your Fortran code instead.
Other than listing the gateway file first so your mex function will have that as its name (the name you will call it from MATLAB), the order of listing the other source files doesn't matter unless you have modules involved, in which case you need to get the modules compiled before you compile other code that depends on those modules.
Also, your compiler should be able to use either .F or .FOR extensions ... the files will be compiled as fixed format. (Although I seem to recall that the mex command might make a distinction between the two for some reason, in which case you might need to rename your files with the .F extension.) If you want to use free format source code you will have to use .F90 extension and modify the MATLAB mex compile file to get rid of the silly /fixed option they keep insisting on having there (it's a bug in their files I have told them about on multiple occasions but they never fix it ... no pun intended). If this is needed let me know because I will have to direct you to the specific file that needs modified.
What version of MATLAB are you using and what specific compiler are you using?
P.S. The timestwo.F example shipped with MATLAB actually has bugs in it and can crash, so be careful if you use this file.