How do i compile multiple fortran code from matlab command
7 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1025970/image.png)
below is the command i usually used to compile those code (generating executable called output.exe)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1025975/image.png)
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 Commenti
James Tursa
il 9 Giu 2022
Modificato: James Tursa
il 9 Giu 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.
Risposte (1)
James Tursa
il 10 Giu 2022
Modificato: James Tursa
il 10 Giu 2022
To pass variables from MATLAB to your Fortran code you would typically do the following:
- 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
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 Commenti
Vedere anche
Categorie
Scopri di più su Fortran with MATLAB in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!