Get from Ico-github-logo

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Join the 15-year community celebration.

Play games and win prizes!

» Learn more

4.15789
4.2 | 19 ratings Rate this file 61 Downloads (last 30 days) File Size: 0 Bytes File ID: #5260 Version: 4.1

benbarrowes/f2matlab

by

Ben Barrowes (view profile)

 

16 Jun 2004 (Updated )

Converts basic fortran90 source code to MATLAB source code.

| Watch this File

File Information
Description

Most recent version at:
http://engineering.dartmouth.edu/~d30574x/consulting/consultingIndex.html
CONTENTS:
-1. SUPPORT f2matlab AND CONSULTING
 0. DISCLAIMER
 1. OBJECTIVE
 2. MOTIVATION
 3. BUG REPORTS and WISH LIST
 4. F2MATLAB CAPABILITIES
 5. F2MATLAB LIMITATIONS
 6. HOW TO USE F2MATLAB
 7. EXAMPLES
 8. REVISION HISTORY
-1.SUPPORT f2matlab.
   I now also do conversion/translation/validation/optimization consulting.
   Please refer to my webpage:
   http://engineering.dartmouth.edu/~d30574x/consulting/consultingIndex.html

   Even though f2matlab is free (under GPL) for the using, I would like
   to ask that those who find it useful, wish to support the project,
   and are able to make a contribution to please do so commesurate with
   use (especially corporations). *** Important - Please donate using
   your PayPal account and not a credit card so as to avoid fees at
   PayPal. Thank you! PayPal email ID: barrowes@alum.mit.edu

0. DISCLAIMER: Matlab is a trademark of the Mathworks company and is
   owned by them. The author makes no guarantee express or implied of
   any kind as to the applicability, usefulness, efficacy,
   bug-freeness, or the accuracy of the ensuing results from using
   f2matlab.

   The author bears no responsibility for any unwanted effect
   resulting from the use of this program. The author is not
   affiliated with the Mathworks. The source code is given in full in
   the hopes that it will prove useful. Devlopment is done through
   sourceforge at f2matlab.sourceforge.net.

1. OBJECTIVE: f2matlab.m is a small translator which aims to
   convert Fortran90 files to Matlab m-files.

2. MOTIVATION:

   1) Matlab is becoming ubiquitous in the engineering and scientific
   communities for its ease of use coupled with its powerful
   libraries. Yet the fact remains that a large number of stable and
   dependable programs exist in the fortran77/90 corpus.

   2) Many times, often amidst the porting of fortran programs to
   Matlab, an automated converter of fortran90 code to Matlab code would
   be useful.

   3) Having written matlab2fmex.m, a matlab to fortran90 mex file
   converter, the writing of f2matlab, which performs the reverse
   conversion, was substantially simplified.

3. BUG REPORTS and WISH LIST:
   For all bug reports, a wish list for f2matlab, and suggestions,
   see http://engineering.dartmouth.edu/~d30574x/consulting/consultingIndex.html
   or email barrowes@alum.mit.edu

4. F2MATLAB CAPABILITIES: f2matlab is aimed at converting
   Fortran90 code to Matlab m-files. Accordingly, only basic data types
   and constructions are recommended. f2matlab can handle:

   all numeric types (handled by Matlab interpreter)
   most string functions
   comparisons, branches, loops, etc.
   basic read/write/print statements (if it's not too fancy...)
   modules

5. F2MATLAB LIMITATIONS: f2matlab can not handle some features of
   fortran90 yet. These include:

   can't handle complex read and write statements
   derived-typed variables
   equivalence
   ...

6. HOW TO USE F2MATLAB: f2matlab expects a single fortran90 fortran file to
   convert. If you have fortran77 code, use some free converter
   (e.g. to_f90 by Alan Miller) before running f2matlab. Then simply
   call f2matlab by using the full filename:
   f2matlab('filename.f90');
   The output will be filename.m in the same directory.
   
   A few flags are available that effect conversion:
   % want_kb=0; 1 ==> if keyboard mode is desired after some conversion steps
   % want_ze=0; 1 ==> direct f2matlab to zero all array variables.
   % want_fi=0; 1 ==> direct f2matlab to try to put fix()'s around declared integers.

   Multiple subroutines and functions can and should be in the same fortran90 file.

Acknowledgements

This file inspired Gauss Hermite and Generalised Exponential Integral.

MATLAB release MATLAB 7 (R14)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (39)
13 Sep 2016 Ben Barrowes

Ben Barrowes (view profile)

Kathryn,

Try tof90 by Alan Miller for a f77 to f90 converter. But I am not sure why you should have this recursion error. Please email me your code at barrowes@alum.mit.edu so I can take a look.

bb

Comment only
12 Sep 2016 Kathryn Purdon

I am getting a recursion error.

Error message: Maximum recursion limit of 500 reached. Use set(0,'RecursionLimit',N) to change the limit.Be aware that exceeding your available stack space can crash MATLAB and/or your computer.

Could this be a result of using fortran77 code?

Do you have a suggestion of a free converter for f77 to f90?

Comment only
07 Sep 2016 Ben Barrowes

Ben Barrowes (view profile)

Maria, Please follow the README. You need to put the directory containing f2matlab on your matlab path.

Comment only
07 Sep 2016 Maria Jose Legaz

I have tried to convert a fortran 95 program.

The error found is:
Undefined function or variable 'f2matlab'

Comment only
06 Sep 2016 Ben Barrowes

Ben Barrowes (view profile)

Chirag, I am not sure why you would have to change the recursion limit. Please send me your code so I can take a look. bb

Comment only
06 Sep 2016 chirag korat

I figure out how to change recursion limit but I can increase this limit to 1000, and it gives error. so in fortran code which thing I should change?

Comment only
06 Sep 2016 chirag korat

can anyone help me out how to change recursion limit during converting fortran to matlab ?

Comment only
18 Jul 2016 Ben Barrowes

Ben Barrowes (view profile)

Michel Hajj (or anyone having trouble with f2matlab), please email me with your fortran file, so I can take a look:
barrowes@alum.mit.edu

Comment only
18 Jul 2016 Kenneth Lamury  
16 Jun 2016 michel hajj

Index exceeds matrix dimensions.

Error in f2matlab (line 4539)
if (strcmp(temp5,'character') || ~isempty(temp6{16})) && length(temp6{5})>0

any help, please?

Comment only
31 May 2016 Flávio cardoso

When when I try run f2matlab to convert fortran in matlab. The screen show me a error below:

??? Undefined function or variable 'getfunwordsonly'.
Error in ==> f2matlab at 103 funwords=getfunwordsonly;)
Anyone can help me? Please

Comment only
20 May 2016 Harikrishna Reddy

Good one

13 Apr 2016 Andrea Libri  
05 Nov 2015 Hassan Mohamed

can anybody please explain step by step the process of conversion, because the readme file doesn't exist.

Comment only
09 Sep 2015 ALIi

ALIi (view profile)

implicit none
REAL U1,U2,r,dt,dx,nu,tf,t,c,X,a,b,d,c2,d2,k,cl,lambda,alpha
integer n,i,j,scheme
ALLOCATABLE U1(:),U2(:),X(:),a(:),b(:),c(:),d(:),c2(:),d2(:)
CHARACTER*80 DON
CHARACTER*80 param
WRITE(*,*) 'Fichier de donnees ? '
READ(*,'(A)') param
OPEN(4,FILE=param)
READ(4,*) n
READ(4,*) dt
READ(4,*) tf
READ(4,*) nu
READ(4,*) scheme
READ(4,*) cl
READ(4,*) lambda
READ(4,*) alpha
CLOSE(4)
IF(scheme.EQ.1) THEN
WRITE(*,*) 'Explicit scheme'
ELSE
WRITE(*,*) 'Implicit scheme'
ENDIF
IF(cl.EQ.0) THEN
WRITE(*,*) 'cl rigide '
ELSE
WRITE(*,*) 'cl libre'
ENDIF
WRITE(*,*) 'n =',n
WRITE(*,*) 'dt =',dt
WRITE(*,*) 'tf =',tf
WRITE(*,*) 'nu =',nu
WRITE(*,*) 'lambda =',lambda
WRITE(*,*) 'alpha =',alpha

dx=1./float(n)
r=nu*dt/(dx*dx)
k=dt/(2*dx)

ALLOCATE (U1(n+1),U2(n+1),X(n+1),a(n+1),b(n+1),c(n+1),
& d(n+1),c2(n+1),d2(n+1))
DO i=1,n+1
X(i)=(i-1)*dx
a(i)=0
b(i)=0
c(i)=0
d(i)=0
END DO
c initialisation
DO i=1,n+1
U1(i)=SIN(2*3.1415927*(i-1)/n)
END DO

t=0
DO WHILE (t.LT.tf)
t=t+dt
c conditions limites
U1(1)=0
U1(n+1)=0
U2(1)=0
U2(n+1)=0

c boucle explicite
IF(scheme.EQ.1) THEN
DO i=2,n
U2(i)=r*U1(i+1)+(1-2*r)*U1(i)+r*U1(i-1)
& -k*U1(i)*(U1(i+1)-U1(i-1))
END DO
DO i=1,n
U1(i)=U2(i)
END DO

c boucle implicite
ELSE

DO j=2,n+1
a(j)=-0.25*dt*U1(j-1)/dx-0.5*r
END DO
DO j=1,n+1
b(j)=1+r
END DO
DO j=1,n
c(j)=0.25*dt*U1(j+1)/dx-0.5*r
END DO
d(1)=(1-r)*U1(1)+0.5*r*U1(2)
d(n+1)=0.5*r*U1(n)+(1-r)*U1(n+1)
DO j=2,n
d(j)=0.5*r*U1(j-1)+(1-r)*U1(j)+0.5*r*U1(j+1)
& +dt*(lambda*U1(j)+alpha*U1(j)*U1(j)*U1(j))
END DO

c algorithme de Thomas
c2(1)=c(1)/b(1)
d2(1)=d(1)/b(1)
DO i=2,n+1
c2(i)=c(i)/(b(i)-a(i)*c2(i-1))
d2(i)=(d(i)-a(i)*d2(i-1))/(b(i)-a(i)*c2(i-1))
END DO
U1(n+1)=d2(n+1)
DO i=1,n
U1(n+1-i)=d2(n+1-i)-U1(n-i+2)*c2(n+1-i)
END DO
ENDIF
END DO

WRITE(*,*) 'Fichier de resultat ? '
READ(*,'(A)') DON
OPEN(5,FILE=DON)
DO i=1,n+1
WRITE(5,*) X(i),U1(i)
END DO
CLOSE (5)

END

Comment only
20 May 2015 alaa ahmed

can any one to convert this code into .m file matlab
thanks
1 DIMENSION A {17,17},B{17,17}
2 DO 6 I=2,17
3 DO 5 J=1,17
4 A{I,J}=0.
5 CONTINUE
6 CONTINUE
7 DO 9 J=2,16
8 A{I,J}=100.
9 CONTINUE
10 A{L,L}=50.
11 A{1,17}=50
12 DO 16 I=2,16
13 DO 15 J=2,16
14 A{I,J}={A{I,J-6}+A{I-L+J}+A{I,J+L}+A{I+L,J}}/4.
15 CONTINUE
16 CONTINUE
17 DO 23 I=2,16
18 DO 22 J=2,16
19 C={A{I,J-L}+A{I-L,J}+A{I,J+L}+A{I,L.J}}/4
20 B{I,J}=A{I,J}-C
21 IF{{ABS{B{I,J}}-.00001}.GT.O.} GO TO 12
22 CONTINUE
23 CONTINUE
24 WRITE{6,25}{{A{I,J},J=1,17},I=1,17}
25 FORMAT {1H0,17F7.2}
26 STOP
27 END

Comment only
03 Nov 2014 Arwan Khoiruddin  
13 Mar 2012 Dikla

Dikla (view profile)

Hi
I have a problem installing this program on Matlab R2011b

Can you provide an explantion of using those files?

Comment only
12 Jul 2011 Jin

Jin (view profile)

 
30 Jun 2011 bruce

bruce (view profile)

Ran the code and immediately received the following error:
??? Index exceeds matrix dimensions.

Error in ==> f2matlab at 1055
funstr={funstr{segmentStarts(whichsub):segmentStarts(whichsub+1)-1}}';

Don't know enough about the code to solve this problem.

Comment only
11 May 2011 AlexanderKospach

I´m trying to convert as well a frotran code to matlab code. I always get the errormessage

---------------------------------------
??? Too many outputs requested. Most likely cause is missing [] around
left hand side that has a comma separated list expansion.

Error in ==> findendSub_f at 80
sublist{slLen+1,7}=sublist{temp6,1};

Error in ==> f2matlab at 899
[sublist,funstr,funstrnumbers,funstrnumbers_b,funstrnumbers_e,funstrwords,funstrwords_b,funstrwords_e,fs_good]=findendSub_f([],sublist,s,funstr,funstrnumbers,funstrnumbers_b,funstrnumbers_e,funstrwo
Error in ==> test at 2
f2matlab('MarqStepEX_P.for');
---------------------------------------

and i have no idea want it means. thx for any help

Comment only
28 Mar 2011 Md mahbub Mishu

I am using this f2matlab file but when I call the function to convert my fortran90 code to .m file, it says "bad cell reference"
I am giving my fortran code below:
C
C RANKINE SOURCE WAVES TRANSFERRED TO FORM KELVIN ANGLE
C (UNIT STRENGTH, UNIT VOLUME)
DIMENSION FSPANL(20,40), FSP2(30,50),
+ A(40,20),B(40,20),H(40,20),
+ AK(40,20),BK(40,20),HK(40,20)
DATA IR,DEPTH/20,20.0/
C
C
C
DO 100 I=1,20
DO 100 J=1,40
100 FSPANL(I,J)=0.0
C
C
C
DELTAR=20.0/FLOAT(IR)
DO 200 I1=1,IR
R=DELTAR*FLOAT(I1)
RK=DELTAR*FLOAT(I1)+0.5
THETA=2.0*3.141592/FLOAT(I1*4)
ITHETA=I1*4
DO 300 I2=1,ITHETA
ANGLE=THETA*FLOAT(I2-1)
A(I2,I1)=R*COS(ANGLE)
AK(I2,I1)=RK*COS(ANGLE)
B(I2,I1)=R*SIN(ANGLE)
BK(I2,I1)=RK*SIN(ANGLE)
300 CONTINUE
C
DO 350 I2=1,ITHETA
R3D=SQRT((A(I2,I1)-A(1,I1))**2+(B(I2,I1)-B(1,I1))**2+DEPTH**2)
R3DK=SQRT((AK(I2,I1)-AK(1,I1))**2
+ +(BK(I2,I1)-BK(1,I1))**2+DEPTH**2)
H(I2,I1)=R3D**2/(2.0*9.81*(R3D**3))
HK(I2,I1)=-R3DK**2/(2.0*9.81*(R3DK**3))
350 CONTINUE
C
DO 360 I2=1,ITHETA
A(I2,I1)=A(I2,I1)+3.0*R
AK(I2,I1)=AK(I2,I1)+3.0*R
B(I2,I1)=B(I2,I1)+10.0
BK(I2,I1)=BK(I2,I1)+10.0
360 CONTINUE
C
C
C
DO 1000 I2=1,ITHETA
C
I=1
J=1
ICOUNT=1
COL=1.0
IF (A(I2,I1).GT.40.0) GO TO 910
400 IF (A(I2,I1).GT.COL) GO TO 500
J=ICOUNT
GO TO 600
500 COL=COL+1.0
ICOUNT=ICOUNT+1
GO TO 400
600 CONTINUE
C
ICOUNT=1
ROW=1.0
IF (B(I2,I1).GT.20.0) GO TO 910
IF (B(I2,I1).LT.0.0) GO TO 910
700 IF (B(I2,I1).GT.ROW) GO TO 800
I=ICOUNT
GO TO 900
800 ROW=ROW+1.0
ICOUNT=ICOUNT+1
GO TO 700
C
910 H(I2,I1)=0.0
900 FSPANL(I,J)=FSPANL(I,J)+H(I2,I1)
C
I=1
J=1
ICOUNT=1
COL=1.0
IF (AK(I2,I1).GT.40.0) GO TO 915
405 IF (AK(I2,I1).GT.COL) GO TO 505
J=ICOUNT
GO TO 605
505 COL=COL+1.0
ICOUNT=ICOUNT+1
GO TO 405
605 CONTINUE
C
ICOUNT=1
ROW=1.0
IF (BK(I2,I1).GT.20.0) GO TO 915
IF (BK(I2,I1).LT.0.0) GO TO 915
705 IF (BK(I2,I1).GT.ROW) GO TO 805
I=ICOUNT
GO TO 905
805 ROW=ROW+1.0
ICOUNT=ICOUNT+1
GO TO 705
C
915 HK(I2,I1)=0.0
905 FSPANL(I,J)=FSPANL(I,J)+HK(I2,I1)
C
1000 CONTINUE
200 CONTINUE
C
C
C
DO 3000 I=1,30
DO 3000 J=1,50
3000 FSP2(I,J)=0.0
C
DO 4000 I=1,20
DO 4000 J=1,40
IP5=I+5
JP10=J+10
4000 FSP2(IP5,JP10)=FSPANL(I,J)
C
C
C
DO 1100 I=1,30
DO 1100 J=1,50
X=0.5+1.0*(J-1)
Y=0.5+1.0*(I-1)
Z=FSP2(I,J)
WRITE(15,5000) X,Y,Z
5000 FORMAT(1X,3F10.6)
1100 CONTINUE

C
C
STOP
END
if anybody can help me to convert this into .m file, I will appreciate his or her effort.
Thanks
Mishu

Comment only
21 Feb 2011 Alan

Alan (view profile)

Hi,

How do I install f2matlab on Vista? I cannot get the "INSTALL" file to execute, there is no 'Run' command when I right-click on it.

Thanks,

Comment only
26 Nov 2010 Erkan Tiryaki

it would be great if I could fix the error points with a guide..

Comment only
14 Jul 2010 pink

pink (view profile)

how to use it, can be given one example of step by step process of using f2matlab

17 Feb 2010 Andre Guy Tranquille  
19 Nov 2009 Hastiepen

I know nothing about Fortran, and had to convert some code into Matlab. The conversions that f2matlab made helped me immensely to give me a starting point. Many thanks!

02 Nov 2009 Martin Richard

Very useful package indeed. Thank you.

19 Apr 2009 Qun HAN

Qun HAN (view profile)

It's great to know this great tool is still imporved.

08 Apr 2008 anonymous anonymous

Hello
I have tried to convert the where statement applied to a 2D array to matlab and it seems f2matlab does not translate well.
Best Regards
Eduardo

program ed_example
integer B(10, 2)

B(1:10, 1) = 0
B(1:10, 2) = 1
where (B(:,:)>0)
B(:,:) = B(:,:) + 9
endwhere
end program

Translating:
f2matlab('ed_example.f')

ans =
b([1:10], 1) = 0;
b([1:10], 2) = 1;
fmask=(b(:,:)>0);
b(fmask,:) = b(fmask,:) + 9;
endwhere;

Running:
>> b(fmask,:) = b(fmask,:) + 9
??? Index exceeds matrix dimensions.

18 Mar 2008 Fat Hidalgo

I tried to run the code but I keep getting the error:

??? Input argument "filename" is undefined.

Error in ==> f2matlab at 101
if exist(filename,'file')==2

I think this is because Matlab does not like when functions are defined in the script. I should define the function outside of the script in a separate .m file, but when I try to do so, I just get more errors. It's hard for to to see what I need to cut from the original program to define the function separately. Anybody with comments on this please email me.

28 Dec 2007 thangjam singh

very good

Comment only
05 Dec 2006 rakesh reddy govindapur

good work

12 Nov 2006 aws ataallah  
08 Nov 2006 Thomas Clark

The actual utility is really useful... When it works.
However, (I think due to the ambiguity between row and column vectors in Fortran) I found a lot of problems in translation, where it just ground to a halt.

In a sense that's to be expected; but there is almost no error handling to cope with that kind of situation- If I could just find out which line of the source code was causing the trouble, I'd be able to comment that section out in the F90 file and manually translate those particular lines. As the f2matlab code is largely uncommented it's havoc to try and trace which lines of code cause the translation problem.

Don't get me wrong; this can be extremely useful code - and clearly a lot of effort has gone into it's development. However, it needs to be a bit more transparent to help us get over the patches where the ambiguity of Fortran lets the translation down.

23 Jun 2006 Anthony Kendall

Though it's not perfect (because of the ambiguity of the GOTO, READ, and WRITE functions in FORTRAN, they don't convert correctly), this converted about 1000 lines of code for me that I was then able to debug and convert to MATLAB format in about 8 hours. Not bad, considering the code was written by someone else!

25 Apr 2006 Malin Premaratne

This is a very useful function

29 Mar 2006 peter spellucci  
07 Mar 2006 shahab ghomi  
Updates

Added module support.

16 Nov 2004

Enhancements, bug fixes.

30 Nov 2004

Enhancements, bug fixes.

09 Dec 2004

Enhancements, bug fixes.
Also added support for simple IO -- read/write/print statments inclusing formatting.

15 Dec 2004

Enhancements, bug fixes.
Added support for implied do loops.

24 Jan 2005

Some bug fixes.

09 Feb 2005

bug fixes, more robust

01 Mar 2005

01/20/05 - some bug fixes
02/08/05 - added try catch's
02/24/05 - added some support for fortran string comparisons, date_and_time
02/25/05 - added some support for functions as arguments

15 Mar 2005

More bug fixes. Support for strings as cells. I/O enhancements.

15 Mar 2005

Many bugs is last dist.

22 Mar 2005

03/09/05 - put fix() around declared integers
03/14/05 - some fixes, loop var now correct on exit from loop
03/21/05 - rewrote some parts to deal with var decs better
save (or declared with a value) vars in fortran are now persistent

30 Mar 2005

Bug fixes.

12 Dec 2005

Some bug fixes. Some enhancements.

14 Dec 2005

Some improvements and fixes.

16 May 2006

Fixed some bugs.
Added matlab reserve word variables changes.

06 Jun 2006

Changed description

31 Aug 2006

Speed improvement.

Bug fixes.

Module capability better.

06 Apr 2007

Bugfixes, added functionality.

16 Oct 2007

updated description

05 Nov 2008 1.1

Some documentation updates and bug fixes.

04 Feb 2009 1.2

bug fixes, features added, more robust

01 May 2009 1.3

Several bug fixes and enhancements. Include files, types, etc.

03 Aug 2016 1.6

Added link to most recent version off site.

17 Aug 2016 4.1

Now on github.

13 Sep 2016 4.1

Updated description.

Contact us