Necessarily Overly Complex: Driving Fortran programs from Matlab

As the title suggests, there are moments in your programming life when you'll need to be overly complex.  Yes, with more time and skill you could likely find a more elegant solution - something you could be proud to stand up and say "Yes! I wrote that!".  But often you'll find an inelegant fix will work just fine all the same. Sure, it's not pretty looking and you wont want to go around showing it off to your more tech savy friends but hey, you wont get any judgement from me! Plus it's likely that only in these moments of patching solutions together that you're really justified in using crass, explicit phrases to comment your code.  Take for example some of the snippets of comments I've found in hand-me-down codes given to me:

"# ---- Reading in ascii file, this will take a stupid long time ... "
"C Dear Jebus please help me write this f*$!ing g77 code"
"C And now, fingers crossed, lets try the whole enchilada "

Ok, but back to the point of this post - driving Fortran (or really any code) from inside Matlab. This sin of programming is definitely my most common instance of sloppy scripting but sometimes you just can't get around it. Recently I have begun testing the robustness of a 3D inversion I'm conducting and to do this I need to generate a large number of different initial models and run them through my inversion scheme.  Given that I'm most comfortable in the soft, soothing confines of Matlab I decided to write my initial models in ascii form with Matlab and then convert them to binary using a fortran program I have (the models have to be in binary to ultimately be fed into the inversion).  Now, I know what you might be asking yourself "Natalie, why not skip the intermediary step and just write the model in binary from the very beginning?!".  To that end, don't get me started on how much time I have spent trying to get Matlab to generate a file in the proper format for my old school Fortran inversion program.  Long story short, we're not going there.  In this instance, the most effective use of my time is to have Matlab quietly chugging away in the background generating a bunch of ascii files and driving a fortran program on the outside to convert those ascii files to binary form.

Phew, a long introduction into the problem at hand! How do you drive Fortran programs from inside of Matlab?  The first and most complex step (which I will make simple) is getting your path setup.  Wait! This is not the same as the path that's set to find executable files on your computer nor is it the same as the path that you set inside of Matlab to help it find other Matlab files.  No, this is a different path altogether.  To set this path you just need to add the following lines to the top of your script where you'll call your program:

% Generate a variable with your current set path
PATH = getenv('PATH');
% Add to that path the path to the program you want o use
setenv('PATH',[PATH ':path-to-your-program']);
% Identify the proper fortran library - dont change this if you have a mac
setenv('DYLD_LIBRARY_PATH', '/usr/local/bin/');

With those at the top of your program you should be able to check to see if Matlab can find the program of interest.  So run your Matlab script or just copy and paste the above commands into the Command Window.  Next run the which command as an external command in the Command Window.  To do this just begin whatever your command would be in the regular terminal but with a leading exclamation point, i.e.:

! which program-of-interest

Now if you've set everything correct Matlab should return the path to the program.  As with most errors in programming, if this doesn't work check your spelling of the paths and commands and try again.

With your paths set you're all ready to actually run the command - this is the simple part! You'll use the Matlab system command which just forces an external command.  To run the system command just put your command of interest inside the trailing parantheses behind system. An easy and fun way to test this might be:

system('echo Hello World')

Now obviously this is not running an external Fortran command but you get the gist.  Just replace the "echo Hello World" with your command and voila! 

I hope this post has been helpful to some of you and perhaps removes a little bit of the embarrassment in messy programing.  I promise we've all been there! Until next time, try out this Matlab trick and happy (sloppy) programming!