Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting MATLAB code to Fortran [closed]

I a medical researcher with code written in MATLAB 2009b that runs very slowly because of a self-referential loop (not sure of the programming lingo here), i.e., the results of the first iteration is used during the second iteration, etc. (I have vectorized it to a fare-thee-well. I have run the Profiler.)

I'd like to convert the slow parts of the code to a mex function. I learned Fortran in the early 1970s but haven't used it since. The code I need to convert doesn't do anything fancy, it is just a long numerical calculation.

My question is: what would be the easiest-to-relearn version of Fortran adequate for this purpose, and what compiler works best on the Intel Mac for this? I found information comparing syntax in MATLAB to Fortran 90 for example, and the conversion doesn't look like it would be too daunting for me. However, again, I am no programmer.

I am using a MacBook Pro with OS 10.6.

Appreciate any help, thanks.

like image 556
Kevin Avatar asked Nov 23 '09 16:11

Kevin


People also ask

Can you call Fortran from MATLAB?

You can call your own Fortran subroutines from the MATLAB command line as if they were built-in functions. These programs are called MEX files, and the function name is the MEX file name. To create a MEX file, write your programs using the MATLAB API libraries — Fortran MEX API and Fortran Matrix API.

Can you convert MATLAB code to C?

To convert MATLAB® Code to fixed-point C Code using the MATLAB Coder™ app: Open the MATLAB Coder app. On the Select Source Files page, add the entry-point function from which you want to generate code. Set Numeric Conversion to Convert to fixed point .

Is Fortran like MATLAB?

For these reasons Matlab is mainly directed to the development prototype, abusing a computer terminology is not for production use. Conversely Fortran is a compiled language in which the development of the code is slower, but has some advantage such as performance.

Can we convert MATLAB code to Python?

To convert Matlab to python, a tool named SMOP (Small Matlab and Octave to Python Compiler) is used. This tool is capable of understanding basic Matlab code and then parsing it to python. Although there are always limitations to every tool, this tool works best for small-level codes.


1 Answers

I'd recommend using modern Fortran, at least 90/95 as the syntax is much more forgiving and almost all compilers now support it.

On a Mac I would recommend gfortran from here. It's not the most recent version, but it's well integrated with Apple build tools (you will need to install Xcode from your Mac OS DVD) and works well. In the numerical python community, which depends a lot of Fortran extensions, this build is highly recommended.

I haven't actually used fortran mex on the mac - but I think it should be fairly straightforward if you follow the mex documentation - and as you say translating code from Matlab to Fortran shouldn't be too bad (it's better if you can avoid calling Matlab functions, but fortran has sensible slicing and array access).

like image 151
robince Avatar answered Sep 27 '22 22:09

robince