Code covered by the BSD License  

Highlights from
f2matlab

4.2

4.2 | 15 ratings Rate this file 190 Downloads (last 30 days) File Size: 1.34 MB File ID: #5260

f2matlab

by Ben Barrowes

 

16 Jun 2004 (Updated 01 May 2009)

Converts basic fortran90 source code to MATLAB source code.

| Watch this File

File Information
Description

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 submission has inspired the following:
GaussHermite
MATLAB release MATLAB 7 (R14)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (21)
07 Mar 2006 shahab ghomi  
29 Mar 2006 peter spellucci  
25 Apr 2006 Malin Premaratne

This is a very useful function

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!

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.

12 Nov 2006 aws ataallah  
05 Dec 2006 rakesh reddy govindapur

good work

28 Dec 2007 thangjam singh

very good

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.

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.

19 Apr 2009 Qun HAN

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

02 Nov 2009 Martin Richard

Very useful package indeed. Thank you.

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!

17 Feb 2010 Andre Guy Tranquille  
14 Jul 2010 pink

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

26 Nov 2010 Erkan Tiryaki

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

21 Feb 2011 Alan

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,

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

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

30 Jun 2011 bruce

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.

12 Jul 2011 Jin  
Please login to add a comment or rating.
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

27 Dec 2007

Many bug fixes and major upgrade of capabilities.

05 Nov 2008

Some documentation updates and bug fixes.

04 Feb 2009

bug fixes, features added, more robust

01 May 2009

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

Tag Activity for this File
Tag Applied By Date/Time
external interface Ben Barrowes 22 Oct 2008 07:24:01
fortran Ben Barrowes 22 Oct 2008 07:24:01
matlab Ben Barrowes 22 Oct 2008 07:24:01
convert Ben Barrowes 22 Oct 2008 07:24:01
utilities Ben Barrowes 22 Oct 2008 07:24:01
translate Ben Barrowes 22 Oct 2008 07:24:01
port Ben Barrowes 04 Feb 2009 15:34:08
convert Witali 13 Feb 2009 10:45:51
external interface Witali 13 Feb 2009 10:45:56
fortran Claudio Gelmi 08 Jun 2010 14:09:27
external interface Mike Liu 24 Apr 2011 22:36:09
fortran Mike Liu 24 Apr 2011 22:36:11
convert Nik Bogoslavski 10 May 2011 09:45:39
convert Jin 12 Jul 2011 22:15:05
convert Mike Liu 12 Oct 2011 20:53:35
translate Mike Liu 12 Oct 2011 20:53:45
convert dfh 04 Jan 2012 09:14:26
convert esogues 29 Jan 2012 15:17:11

Contact us at files@mathworks.com