- READ with input,
- WRITE with fprintf,
- endif with end,
- .EQ. with ==,
- .GT. with >.
- ! with %
Convert Fortran to MATLAB
11 views (last 30 days)
Show older comments
I like to convert the following fortran code to matlab, I need help please:
!*PROGRAM TO COVERT SOUNDING DATA TO 2D RESISTIVITY DATA FILE *!
!------------------------------------------------------------------------!
IMPLICIT NONE
REAL::A,X,R,L,LN,N1,N2,LT
CHARACTER :: HEAD
CHARACTER(Len = 20) :: FNI,FNO,LOC
INTEGER :: I,N,SP,SPN, AT
!------------------------------------------------------------------------!
WRITE(*,*) ‘ENTER INPUT FILE NAME (CASE SENSITIVE):’
READ(*,*) FNI
WRITE(*,*) ‘ENTER OUTPUT FILE NAME:’
READ(*,*) FNO
WRITE(*,*)’For Wenner=1;DipoleDipole=3,Schlumberger=7’
WRITE(*,*)’ENTER ARRAY TYPE:’
READ(*,*) AT
WRITE(*,*) ‘ENTER NO OF DATA POINTS:’
READ(*,*) N
WRITE(*,*) ‘ENTER MINIMUM SPACING:’
READ(*,*) A
OPEN (1,FILE=FNI,STATUS=’OLD’)
OPEN(2,FILE=FNO,STATUS=’NEW’)
READ(1,’(A)’) LOC
WRITE(2,*) LOC
READ(1,’(A)’) HEAD
WRITE(2,*) A
WRITE(2,*) AT
WRITE(2,*) N
WRITE(2,*) ‘1’
WRITE(2,*) ‘0’
SP=0
N2=0
DO 10 I=1,N
READ (1,*) LT,LN,X,R
N1=X/A
IF (N1.LT.N2) THEN
SP=SP+1
ENDIF
L=SP*A
N2=N1
IF (AT.EQ.1) THEN
WRITE(2,*) L,A,R
ENDIF
IF (AT.GT.1) THEN
WRITE(2,*) L,A,N2,R
ENDIF
10 CONTINUE
WRITE(2,*) ‘0’
WRITE(2,*) ‘0’
WRITE(2,*) ‘0’
CLOSE(1)
CLOSE(2)
END
!------------------------------------------------------------------------!
0 Comments
Accepted Answer
Image Analyst
on 28 Aug 2021
DO 10 I=1,N
goes to
for i = 1 : N
Replace
and so on. Here's a start (not working yet). I trust you'll be able to finish it
% PROGRAM TO COVERT SOUNDING DATA TO 2D RESISTIVITY DATA FILE
%------------------------------------------------------------------------
FNI = input('ENTER INPUT FILE NAME (CASE SENSITIVE):', 's')
FNO = input('ENTER OUTPUT FILE NAME:', 's')
fprintf('For Wenner=1;DipoleDipole=3,Schlumberger=7\n');
AT = input('ENTER ARRAY TYPE:')
N = input('ENTER NO OF DATA POINTS:')
A = input('ENTER MINIMUM SPACING:')
fh1 = fopen(FNI,'rt')
fh2 = fopen(FNO,'wt')
fread(fh1,'(A) %f\n', LOC)
fprintf(fh2, '%f\n', LOC)
fread(fh1, '(A) %f\n', HEAD)
fprintf(fh2, '%f\n', A)
fprintf(fh2, '%f\n', AT)
fprintf(fh2, '%f\n', N)
fprintf(fh2, '%f\n', 1)
fprintf(fh2, '%f\n', 0)
SP = 0
N2 = 0
for I = 1 : N
fread(fh1, '%f',LT,LN,X,R);
N1 = X / A
if (N1 < N2)
SP=SP+1
end
L = SP*A
N2 = N1
if (AT == 1)
fprintf(fh2, '%f, ', L,A,R)
end
if (AT > 1)
fprintf(fh2, '%f,', L,A,N2,R)
end
end
fprintf(fh2, '0\n0\n0\n')
fclose(fh1)
fclose(fh2)
I didn't do it all for you, so don't say "It doesn't work" because I know it doesn't work. Just run it and fix all remaining errors as they occur. A good way to learn MATLAB (well at least A way to learn MATLAB).
0 Comments
More Answers (1)
Ben Barrowes
on 18 Nov 2022
Geopaul,
I used my f2matlab on your code to convert the fortran to matlab code. There are a few helper functions (like writeFmt) which are included in the attached zip file. Here is the resulting matlab code below.
%
%*PROGRAM TO COVERT SOUNDING DATA TO 2D RESISTIVITY DATA FILE *!
%------------------------------------------------------------------------!
function sound2d(varargin)
%
%
clear global;
clear functions;
global GlobInArgs nargs
GlobInArgs={mfilename,varargin{:}};
nargs=nargin+1;
global unit2fid;
if ~isempty(unit2fid);
unit2fid={};
end
persistent a at firstCall fni fno head i l ln loc_ml lt_fv n n1 n2 r sp spn x
;
if isempty(firstCall);
firstCall=1;
end;
if firstCall;
a=0.0;
at=0;
fni=blanks(20);
fno=blanks(20);
head=blanks(1);
i=0;
l=0.0;
ln=0.0;
loc_ml=blanks(20);
lt_fv=0.0;
n=0;
n1=0.0;
n2=0.0;
r=0.0;
sp=0;
spn=0;
x=0.0;
end
firstCall=0;
%------------------------------------------------------------------------!
[writeErrFlag,wfso]=writeFmt(1,['%v'],'''ENTER INPUT FILE NAME (CASE SENSITIVE):''');
eval(wfso);
fni=strAssign(fni,[],[],input('','s'));
[writeErrFlag,wfso]=writeFmt(1,['%v'],'''ENTER OUTPUT FILE NAME:''');
eval(wfso);
fno=strAssign(fno,[],[],input('','s'));
[writeErrFlag,wfso]=writeFmt(1,['%v'],'''For Wenner=1;DipoleDipole=3,Schlumberger=7''');
eval(wfso);
[writeErrFlag,wfso]=writeFmt(1,['%v'],'''ENTER ARRAY TYPE:''');
eval(wfso);
at=fix(str2num(input('','s')));
[writeErrFlag,wfso]=writeFmt(1,['%v'],'''ENTER NO OF DATA POINTS:''');
eval(wfso);
n=fix(str2num(input('','s')));
[writeErrFlag,wfso]=writeFmt(1,['%v'],'''ENTER MINIMUM SPACING:''');
eval(wfso);
a=str2num(input('','s'));
thismlfid=fopen(strtrim(fni),'r+');
unit2fid{end+1,1}=1001;
unit2fid{end,2}=thismlfid;
unit2fid{end,3}=0;
unit2fid{end,4}=strtrim(fni);
unit2fid{end,5}=0;
unit2fid{end,6}=0;
% unit2fid maps fortran unit numbers to matlab fid numbers
thismlfid=fopen(strtrim(fno),'w+');
unit2fid{end+1,1}=2;
unit2fid{end,2}=thismlfid;
unit2fid{end,3}=0;
unit2fid{end,4}=strtrim(fno);
unit2fid{end,5}=0;
unit2fid{end,6}=0;
% unit2fid maps fortran unit numbers to matlab fid numbers
[readErrFlag,readEndFlag,rfso,iostatdum]=readFmt(1001,['%c'],'loc_ml');
eval(rfso);
[writeErrFlag,wfso]=writeFmt(2,['%r'],'loc_ml');
eval(wfso);
[readErrFlag,readEndFlag,rfso,iostatdum]=readFmt(1001,['%c'],'head');
eval(rfso);
[writeErrFlag,wfso]=writeFmt(2,['%w'],'a');
eval(wfso);
[writeErrFlag,wfso]=writeFmt(2,['%w'],'at');
eval(wfso);
[writeErrFlag,wfso]=writeFmt(2,['%w'],'n');
eval(wfso);
[writeErrFlag,wfso]=writeFmt(2,['%v'],'''1''');
eval(wfso);
[writeErrFlag,wfso]=writeFmt(2,['%v'],'''0''');
eval(wfso);
sp = 0;
n2 = 0;
for i = 1: n
[readErrFlag,readEndFlag,rfso,iostatdum]=readFmt(1001,['%w','%w','%w','%w'],'lt_fv','ln','x','r');
eval(rfso);
n1 = x./a;
if(n1 < n2)
sp = fix(sp + 1);
end
l = sp.*a;
n2 = n1;
if(at == 1)
[writeErrFlag,wfso]=writeFmt(2,['%w','%w','%w'],'l','a','r');
eval(wfso);
end
if(at > 1)
[writeErrFlag,wfso]=writeFmt(2,['%w','%w','%w','%w'],'l','a','n2','r');
eval(wfso);
end
end
i = fix(n+1);
[writeErrFlag,wfso]=writeFmt(2,['%v'],'''0''');
eval(wfso);
[writeErrFlag,wfso]=writeFmt(2,['%v'],'''0''');
eval(wfso);
[writeErrFlag,wfso]=writeFmt(2,['%v'],'''0''');
eval(wfso);
try %try closing the file
fclose(unit2fid{[unit2fid{:,1}]==1,2});
if iscell(unit2fid{[unit2fid{:,1}]==1,6}) % is this a direct access file?
writecell(strtrim(unit2fid{[unit2fid{:,1}]==1,6}),'zztempfile.txt');
movefile('zztempfile.txt',strtrim(unit2fid{[unit2fid{:,1}]==1,4}));
end
unit2fid=unit2fid([unit2fid{:,1}]~=1,:);
catch
end
try %try closing the file
fclose(unit2fid{[unit2fid{:,1}]==2,2});
if iscell(unit2fid{[unit2fid{:,1}]==2,6}) % is this a direct access file?
writecell(strtrim(unit2fid{[unit2fid{:,1}]==2,6}),'zztempfile.txt');
movefile('zztempfile.txt',strtrim(unit2fid{[unit2fid{:,1}]==2,4}));
end
unit2fid=unit2fid([unit2fid{:,1}]~=2,:);
catch
end
closeAllOpenFiles(unit2fid);
end %program sound2d
%------------------------------------------------------------------------!
0 Comments
See Also
Categories
Find more on Fortran with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!