How do i compile multiple fortran code from matlab command

9 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)
the question:
how do i compile these multi-script program just from matlab.
thank you for whoever considering want to help me
  2 Comments
James Tursa
James Tursa on 9 Jun 2022
Edited: James Tursa on 9 Jun 2022
It is unclear what you really need here. If you have the standalone executable already there, do you simply need to invoke it from MATLAB? E.g., using the exclamation point ! to run a system command:
Or do you intend to pass variables from MATLAB to your program? In which case you would need to write a mex interface:
The mex command can compile multiple source files containing multiple subroutines ... you don't need to combine all of your subroutines into one subroutine, and you don't need to combine all your source files into one source file.
Please provide more details of what is actually needed.
Mahardika Firjatullah
Mahardika Firjatullah on 10 Jun 2022
yes your are right, i was not being specific the last time. i want to pass variables (a constant and an array) into the fortran programs.
now to be specific about what i want to ask
do i need to transfrom those 5 .FOR files into MEX format, or do i just need to write 1 specific MEX format fortran code additional to my fortran codes? (maybe you have previous experience similar to mine)
also all the example that i read use .F extension while mine is .FOR. does that make any differences in the process?
thank you for responding to my questions :)

Sign in to comment.

Answers (1)

James Tursa
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:
  1. Write a single gateway routine in a separate file that contains the subroutine mexFunction(...)
  2. In the mexFunction(...) routine, extract the inputs and pass them to your existing Fortran code
  3. Modify your existing Fortran code to accept the inputs from the mexFunction(...) subroutine
  4. Use the mex command to compile your code, listing the gateway file first and all your other files last
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.
  2 Comments
Mahardika Firjatullah
Mahardika Firjatullah on 11 Jun 2022
thank you for your direction, i will try it first and ask you later if i still stuck.
btw what is FEX for example? where can i access it?
Mahardika Firjatullah
Mahardika Firjatullah on 11 Jun 2022
@James Tursa now i already facing my first problem, sorry if my questions kind of no brainer for you. i dont have much experience in coding.
i face a problem during linking matlab to my fortran compiler. so my fortran compiler i use the following compiler version (checked using gfortran -v in command prompt)
but when i try to use
mex -setup
i got the following message
when i click the FORTRAN selection i got error messages.
what should i do to tackle this problem?

Sign in to comment.

Categories

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

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!