MATLAB: FORTRAN code in MATLAB

convertconverterexternal interfacef2matlabfortranmextranslate

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!

Best Answer

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.
Related Question