Code covered by the BSD License  

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

» Watch video

Highlights from
f2matlab

4.15789
4.2 | 19 ratings Rate this file 86 Downloads (last 30 days) File Size: 1.34 MB File ID: #5260 Version: 4.1
image thumbnail

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
Unzip in its own directory. Read the README file.
4. F2MATLAB CAPABILITIES: f2matlab is aimed at converting numerical 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, types, etc. basic read/write/print statements (if it's not too fancy...) 5. F2MATLAB LIMITATIONS: f2matlab can not handle some features of fortran90 yet. These include: can't handle difficult IO and read and write statements, goto (of course), equivalence, and pointers.

Acknowledgements

This file inspired Gauss Hermite and Generalised Exponential Integral.

MATLAB release MATLAB 9.0 (R2016a)
MATLAB Search Path
/
/f2matlab
/f2matlab/comp_spec_func
/f2matlab/examples
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (32)
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.

01 Aug 2012 1.6

Added link to most recent version off site.

18 Jul 2016 4.1

Some bug fixes.

18 Jul 2016 4.1

updated the description

25 Jul 2016 4.1

Fixed some file dependency bugs.

25 Jul 2016 4.1

removed some linux executables from the zip file

Contact us