# How to convert a big Fortran program into Matlab?

**URL:** <https://fortran-lang.discourse.group/t/how-to-convert-a-big-fortran-program-into-matlab/7700>\
**Category:** Uncategorized\
**Created:** [March 26, 2024, 1:26pm UTC](https://fortran-lang.discourse.group/t/how-to-convert-a-big-fortran-program-into-matlab/7700 "2024-03-26T13:26:31Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![David91](https://avatars.discourse-cdn.com/v4/letter/d/ee7513/32.png) [@David91](https://fortran-lang.discourse.group/u/David91)\
**Post date:** [March 26, 2024, 1:26pm UTC](https://fortran-lang.discourse.group/t/how-to-convert-a-big-fortran-program-into-matlab/7700/1 "2024-03-26T13:26:31Z")

</div>

Hello everyone,  
I am a student, seeking a support to achieve my research please.  
I have Fortran program (about 2140 lines) that returns to 1992. I want to convert it to Matlab but still struggling to find a proper way. I tried Online Fortran to Matlab Converter but the maximum it can convert is 200 lines. I believe if I convert each 200 line then it loses the program structure.  
Besides, I’m beginner in Fortran.

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [March 26, 2024, 1:41pm UTC](https://fortran-lang.discourse.group/t/how-to-convert-a-big-fortran-program-into-matlab/7700/2 "2024-03-26T13:41:57Z")

</div>

Do you really need to convert the Fortran program to Matlab, or could you use [Fortran MEX API - MATLAB & Simulink](https://www.mathworks.com/help/matlab/mex-library-fortran.html)? For conversion there is

[f2matlab](https://github.com/benbarrowes/f2matlab): converts Fortran 90 code to Matlab m-files, by benbarrowes. Accordingly, only basic data types and constructions are recommended.

Are you able to compile the Fortran program with gfortran or another compiler? You need to do that to check that a Matlab version works correctly. If the program is from 1992 it is likely written in Fortran 77 or an earlier version of Fortran. There are Fortran 77 tutorials at the [Fortran Wiki](https://fortranwiki.org/fortran/show/Tutorials).

---

<div class="post-metadata">

**Author:** ![Jweber](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@Jweber](https://fortran-lang.discourse.group/u/Jweber)\
**Post date:** [March 26, 2024, 2:14pm UTC](https://fortran-lang.discourse.group/t/how-to-convert-a-big-fortran-program-into-matlab/7700/3 "2024-03-26T14:14:50Z")

</div>

I do not know whether it might be useful to try to understand the program and then to rewrite it manually.

---

<div class="post-metadata">

**Author:** ![sshestov](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@sshestov](https://fortran-lang.discourse.group/u/sshestov)\
**Post date:** [March 27, 2024, 10:08am UTC](https://fortran-lang.discourse.group/t/how-to-convert-a-big-fortran-program-into-matlab/7700/4 "2024-03-27T10:08:07Z")

</div>

Can you lift the veil of uncertainty about the program? Among us there are specialists in various fields, perhaps they can also give advice.

---

<div class="post-metadata">

**Author:** ![benbarrowes](https://avatars.discourse-cdn.com/v4/letter/b/838e76/32.png) [@benbarrowes](https://fortran-lang.discourse.group/u/benbarrowes)\
**Post date:** [March 27, 2024, 10:30pm UTC](https://fortran-lang.discourse.group/t/how-to-convert-a-big-fortran-program-into-matlab/7700/5 "2024-03-27T22:30:51Z")

</div>

I can help with f2matlab if it has a problem during the conversion. PM if you have a problem, once you have tried f2matlab from the matlab file exchange.

---

<div class="post-metadata">

**Author:** ![David91](https://avatars.discourse-cdn.com/v4/letter/d/ee7513/32.png) [@David91](https://fortran-lang.discourse.group/u/David91)\
**Post date:** [March 29, 2024, 3:00pm UTC](https://fortran-lang.discourse.group/t/how-to-convert-a-big-fortran-program-into-matlab/7700/7 "2024-03-29T15:00:45Z")

</div>

What you mean by " lift the veil of uncertainty " ?

---

<div class="post-metadata">

**Author:** ![David91](https://avatars.discourse-cdn.com/v4/letter/d/ee7513/32.png) [@David91](https://fortran-lang.discourse.group/u/David91)\
**Post date:** [March 29, 2024, 3:01pm UTC](https://fortran-lang.discourse.group/t/how-to-convert-a-big-fortran-program-into-matlab/7700/8 "2024-03-29T15:01:32Z")

</div>

I would appreciate that. Thank you  
I installed f2matlab in macbook, but I don’t know how to use it despite I read readme file.  
I can open f2matlab.m file but not clear for the next step.

Please advise

---

<div class="post-metadata">

**Author:** ![David91](https://avatars.discourse-cdn.com/v4/letter/d/ee7513/32.png) [@David91](https://fortran-lang.discourse.group/u/David91)\
**Post date:** [March 29, 2024, 3:04pm UTC](https://fortran-lang.discourse.group/t/how-to-convert-a-big-fortran-program-into-matlab/7700/9 "2024-03-29T15:04:52Z")

</div>

The program is more than 2140 lines. Besides, Im beginner in Fortran and running out of time to finish my research.

---

<div class="post-metadata">

**Author:** ![sshestov](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@sshestov](https://fortran-lang.discourse.group/u/sshestov)\
**Post date:** [March 29, 2024, 3:25pm UTC](https://fortran-lang.discourse.group/t/how-to-convert-a-big-fortran-program-into-matlab/7700/10 "2024-03-29T15:25:47Z")

</div>

Tell us the name of the program and from which field of the science it is 🙂

---

<div class="post-metadata">

**Author:** ![benbarrowes](https://avatars.discourse-cdn.com/v4/letter/b/838e76/32.png) [@benbarrowes](https://fortran-lang.discourse.group/u/benbarrowes)\
**Post date:** [March 29, 2024, 3:49pm UTC](https://fortran-lang.discourse.group/t/how-to-convert-a-big-fortran-program-into-matlab/7700/11 "2024-03-29T15:49:26Z")

</div>

David91. Put f2matlab on your matlab path (see pathtool). Then use f2matlab like this:

f2matlab(‘yourFortranFileName’)

And, like I mentioned, you can PM (private mail) me at: barrowes@alum.mit.edu

---

<div class="post-metadata">

**Author:** ![David91](https://avatars.discourse-cdn.com/v4/letter/d/ee7513/32.png) [@David91](https://fortran-lang.discourse.group/u/David91)\
**Post date:** [March 31, 2024, 11:01pm UTC](https://fortran-lang.discourse.group/t/how-to-convert-a-big-fortran-program-into-matlab/7700/13 "2024-03-31T23:01:32Z")

</div>

I want to run the program in matlab platform, either converted directly to matlab code or called through MEX. The purpose to use Matlab is to simplify plotting the graphs.

I tried with MEX but seemed quite difficult to continue.  
I followed instructions from the link " [Create Fortran Source MEX File - MATLAB & Simulink - MathWorks Nordic](https://se.mathworks.com/help/matlab/matlab_external/create-fortran-source-mex-file.html#d126e43171)", but it did not work for configuring MEX as follows :

> > mex -R2023b timestwo.F  
> > Error using mex  
> > Unknown MEX argument ‘-R2023b’.

---

<div class="post-metadata">

**Author:** ![David91](https://avatars.discourse-cdn.com/v4/letter/d/ee7513/32.png) [@David91](https://fortran-lang.discourse.group/u/David91)\
**Post date:** [March 31, 2024, 11:02pm UTC](https://fortran-lang.discourse.group/t/how-to-convert-a-big-fortran-program-into-matlab/7700/14 "2024-03-31T23:02:50Z")

</div>

It calculates attenuation of EM.

---

<div class="post-metadata">

**Author:** ![zaikunzhang](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/zaikunzhang/32/785_2.png) [@zaikunzhang](https://fortran-lang.discourse.group/u/zaikunzhang)\
**Post date:** [March 31, 2024, 11:34pm UTC](https://fortran-lang.discourse.group/t/how-to-convert-a-big-fortran-program-into-matlab/7700/15 "2024-03-31T23:34:06Z")

</div>

Note that the `-R2018a` in the example is not to indicate the version of your MATLAB (it would be bizarre if MATLAB actually needed us to indicate that, no?). See

> **[Build MEX function or engine application - MATLAB mex](https://www.mathworks.com/help/matlab/ref/mex.html)**
>
> This MATLAB function compiles and links one or more C++ source files written with the MATLAB Data API for C++ into a binary MEX file in the current folder.

Consequently, you should not use `-R2023b`, which is not a valid option. Just remove it.
