Converting legacy FORTRAN code to MATLAB code
26 views (last 30 days)
Show older comments
Ken Bannister
on 17 Aug 2022
Commented: Ken Bannister
on 24 Aug 2022
I have found Ben Barrowes' f2matlab program for converting legacy FORTRAN to MATLAB.
Has anyone actually used this software successfully? When I try to run it from the command prompt:
f2matlab('myprogram.f90')
or
f2matlab('myprogram.f')
where myprogram.90 or myprogram.f are in the correct location and are found, the code blows up right away on a disp statement with the following error message. It looks like it tries to read the first statements of the .f90 or .f files as a filename:
************************************************************************************************************************************************************
-----------------------------------------------------------
| f2matlab -- Ben Barrowes, Barrowes Consulting |
-----------------------------------------------------------
Not enough input arguments.
Error in f2matlab (line 225)
disp(['Before includes ',filename,' (',num2str(cputime-tStart),' elapsed)']);
*************************************************************************************************************************************************************
Anyway, does anyone know what is going on?
0 Comments
Accepted Answer
Cris LaPierre
on 17 Aug 2022
Ben seems to be responsive to Discussion posts on the corresponding File Exchange page. You might try posting your question there.
5 Comments
Ben Barrowes
on 19 Aug 2022
Ken: your code has a couple of issues that the free fileexchange version of f2matlab can't handle. In the README file it says that the file needs to have all goto statements (including arithmetic if statements) refactored before f2matlab can work. This is an "arithmetic if" in your code:
if(w(j))5,10,10
And then you also have a goto statement "go to 15" in your code.
I have a more capable version of f2matlab, and here is the result for your code:
function [n1,w_fv,s,n2,nout]=print1(n1,w_fv,s,n2,nout,varargin);
persistent firstCall i j v1 format_25 format_30 format_35
if isempty(firstCall);
firstCall=1;
end;
if firstCall;
format_25=[ '\n' , '\n' , '\n' ,'%1x',' mode shapes'];
format_30=[ '\n' ,'%1x',' mode number','%3i',' ferquency ','%12.4e'];
format_35=['%1x',repmat('%12.4e',1,4)];
i=0;
j=0;
v1=0.0;
end
firstCall=0;
[s,s_orig,s_shape]=reshapeinout(s,[20,20]);
[writeErrFlag,wfso]=writeFmt(nout,[format_25]);
eval(wfso);
for j = 1: n1
if(w_fv(j) < 0)
v1 = -sqrt(-w_fv(j))./6.2831853;
else;
v1 = sqrt(w_fv(j))./6.2831853;
end
[writeErrFlag,wfso]=writeFmt(nout,[format_30],'j','v1');
eval(wfso);
[writeErrFlag,wfso]=writeFmt(nout,[format_35],{'s(i,j)','i',1,1,n2});
eval(wfso);
end
j = fix(n1+1);
s=reshapeinout(s,s_orig,s_shape);
end %subroutine print1
I have attached a zip file of some auxiliary functions that this routine will also need (writeFmt.m, reshapeinouts.m, etc.).
Some of these lines are probably overkill for your code (reshaping inputs and outputs), but fortran can do some things that matlab can't (and probably shouldn't) do. For example, in fortran, you could pass just a 1-D vector 400 values (or longer!) long into your 3rd input, s, and fortran would be fine with it. Not so in matlab. Thus the reshaping just in case. If I turn this functionaliy off, the code would look like this:
function [n1,w_fv,s,n2,nout]=print1(n1,w_fv,s,n2,nout,varargin);
persistent firstCall i j v1 format_25 format_30 format_35
if isempty(firstCall);
firstCall=1;
end;
if firstCall;
format_25=[ '\n' , '\n' , '\n' ,'%1x',' mode shapes'];
format_30=[ '\n' ,'%1x',' mode number','%3i',' ferquency ','%12.4e'];
format_35=['%1x',repmat('%12.4e',1,4)];
i=0;
j=0;
v1=0.0;
end
firstCall=0;
[writeErrFlag,wfso]=writeFmt(nout,[format_25]);
eval(wfso);
for j = 1: n1
if(w_fv(j) < 0)
v1 = -sqrt(-w_fv(j))./6.2831853;
else;
v1 = sqrt(w_fv(j))./6.2831853;
end
[writeErrFlag,wfso]=writeFmt(nout,[format_30],'j','v1');
eval(wfso);
[writeErrFlag,wfso]=writeFmt(nout,[format_35],{'s(i,j)','i',1,1,n2});
eval(wfso);
end
j = fix(n1+1);
end %subroutine print1
More Answers (1)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!