FORTRAN code in MATLAB

17 次查看(过去 30 天)
michaelborg
michaelborg 2011-12-7
评论: roborrr 2023-9-13
I need to integrate a FORTRAN code into MATLAB. Which of the following options is more reliable, efficient (performance-wise), etc. to carry out this task?
1. Create a program in MATLAB that calls the FORTRAN program through the mex functions.
2. completely translate/convert the FORTRAN code into MATLAB code using the f2matlab function (or some other converter)
Thanks in advance!
  4 个评论
Walter Roberson
Walter Roberson 2015-8-26
Sumit: which of those is true for your code? Is it like a function? Or is it interactive with the user?
Sumit Pal
Sumit Pal 2015-8-27
It is interactive with the user. How can I proceed?

请先登录,再进行评论。

采纳的回答

Dr. Seis
Dr. Seis 2011-12-7
I am running on a unix machine. If I had Matlab code that depends on the output of Fortran code (and I didn't want to spend time either creating a usuable MEX file or creating a slower Matlab version of it), then I would set up the Matlab code to simply run the Fortran code in an xterm:
unix('xterm -e /path_to_fortran_code/my_fortran_executable my_fortran_args &');
This will launch 1 instance of the fortran executable in an xterm, which should be able to run on its own CPU by itself. However, Matlab will not wait for your run to finish. To have your Matlab code setup to wait for the results, you will have to add something to your Matlab code (i.e., like reading a text file) that can check to see if the Fortran code has finished. An example would be, have Matlab create a temporary text file with a '0' in it. Then add to the end of your Fortran code a line that overwrites the temporary text file with a '1' once the execution has completed. Your Matlab code would look something like:
fid = fopen('temp001.txt','w');
fprintf(fid,'0');
fclose(fid);
unix('...'); % you fill in the "..." with xterm -e blah blah
program_done = 0;
while program_done == 0
pause(10); % pauses program 10 seconds before checking temp001.txt again
fid = fopen('temp001.txt','r');
program_done = fscanf(fid,'%d');
fclose(fid);
end
Then open and read file associated with output from Fortran execution.
  2 个评论
Titus Edelhofer
Titus Edelhofer 2011-12-7
Hi Elige,
interesting. What is the advantage of doing this compared to just do something like
result = system('/path_to_fortran_code/the_executable params');
Titus
Dr. Seis
Dr. Seis 2011-12-7
编辑:Dr. Seis 2012-10-31
I had a case where I wanted to launch multiple instances of the same program, but with different input files (say 4 total). In this case, they had to run in separate windows in order to all run together at the same time. To do this I set up the string inside "unix()" like this:
unix('xterm -e /path_to_exe/my_exe file001.txt &; xterm -e /path_to_exe/my_exe file002.txt &; xterm -e /path_to_exe/my_exe file003.txt &; xterm -e /path_to_exe/my_exe file004.txt &;');
I think if I didn't have the xterms for each individual run, the executions would end up running chaotically together on one CPU (I may be wrong). The other advantage is that (I think) Matlab can continue doing other separate processing steps on its CPU that it may need to do before the output from the external executable is finished.

请先登录,再进行评论。

更多回答(1 个)

Dr. Seis
Dr. Seis 2011-12-7
In my experience, Matlab will most likely be slower than its Fortran equivalent. Instead of trying to manipulate the original Fortran code so that it is able to run as a MEX file, which I did and was a real pain in the rear, you might try having your Matlab program simply execute the Fortran code outside of Matlab (if your system has > 1 cpu) and then just have your Matlab program set up to wait for the program to finish.
I can elaborate a little more if this sounds like a possible 3rd alternative. What type of system are you running on?
  2 个评论
michaelborg
michaelborg 2011-12-7
I have access to parallel computing, so using multiple processors is not a problem. Can you elaborate on your suggestion please?
roborrr
roborrr 2023-9-13
What commands can be used to rewrite the value of the temporary file temp001.txt "0" to "1" in Fortran?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Fortran with MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by