File Exchange

image thumbnail


version (156 KB) by Ben Barrowes
matlab2fmex.m is a small translator which aims to convert numerical M-files to Fortran90 mex.


Updated 23 Nov 2005

View License

Editor's Note: This author's upload was somehow corrupted. We fixed this on March 29, 2006. At that time, we also converted it from a GZ file a ZIP file for better cross platform portability.

matlab2fmex.m is a small translator which aims to convert numerical MATLAB m-files to Fortran90 mex files. matlab2fmex first translates an m-file to a Fortran90 mex source file then compiles that Fortran90 file using MATLAB's mex and the local compiler. Also available at the sourceforge:

Cite As

Ben Barrowes (2021). matlab2fmex (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (8)


Something I learned through this exercise is that Fortran 90 apparently provides
array programming features as in the following line of code:


which computes the $L_{dim}$ norm of the $1 \times n$ array in_1,
without the use of any loop.


I find this a very limited tool;
it is by far not robust and the 'documentation'
is not around the right priorities.

Admittedly, it's a tough job to translate between
Matlab = an array programming language with zillions of readily available 'library' functions


Fortran = a strong typed language, whose even later flavours may still be a bit 'deficient' as to handling
and declaring arrays; not to mention that one needs the Diogenes lantern to look around for
zillions of libraries, scattered around the Net (i.e NOT readily available !), that barely implement
a subset of Matlab's 'core' vector-matrix math

As said, I find the approach taken by matlab2fmex as utmostly unappealing in its:

* all-or-nothing approach: you won't get a single .F90 line out unless you:
a) think small, and
b) are ready to go back
to the drawing board of your ORIGINAL Matlab code to make it compatible with the tons of matlab2fmex's
(sometimes very unintuitive) rigid constraints

* that a long list of Matlab-inspired operator-primitives will accompany every teeny bit of user function;
instead of having directed the user to a matlab2fmex 'standard' .F90 module, from which to pick primitives that are ACTUALLY NEEDED.

* that it ambitions to go all the way to actual mex compilation:
a) this is a non-sequitur (see above)
b) it loads the translations with yet another layer (the mex one) of human-unintelligible .F90 code

* All this is made 'turn key' for a beginner in both Matlab and Fortran?!
No kidding guys!


Overall: yet another poor semi-automatic translator

John Brown

The final version I found at is 1.21, which is released in 2005. Is there newer version of this GREAT tool!? I love this tool very much. Please DO NOT stop to update it.

jun ziyang

Great tools!
But I found it can't deal with the following kind of code:

pt(indFPS,1) = signalGenerator(t,...

Anyone knows how to modify matlab2fmex to do the job?


Michal Kvasnicka

Very useful tool for number crunching apps.

Egemen Kolemen

There is a problem with the "matlab2fmex" function's call to regexp & regexprep. I am using matlab 6.5. You need to use 'tokenize' options in order to specify '$' in these two matlab functions. Below I posted the fixed version. All I did was to add a 'tokenize' option in all the calls to regexp & regexprep then everything works fine.

2 more problems you may face while using these functions:
1. the variables are not case sensitive. if you use both t and T as variables in the m file, you'll get an error.
2. don't do any calculations within the brackest i.e. convert d=[ a; a+b] to c=a+b; d=[a;c];
Additionally, the convertion make a local copy of the data before calling the function to be evaluated which is less than optimal when dealing with big data sets. You can avoid this by using pointers. See
for details.

Christopher Hoen

How do I extract the content of the file that I download?

Have tried Cygwin tar and WinZip, but noen of them works. The name of the file seems strange also:


turbulence student

thanks a lot!

MATLAB Release Compatibility
Created with R12
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!