MATLAB Answers

Jessica
0

Fortran to matlab conversion

Asked by Jessica
on 16 Jul 2012
Hi, I am trying to convert a code from Fortran to MATLAB and I am stuck. I basically need to output the results, using write command in Fortran but within a loop. Does anyone know how to do this in MATLAB? Below is the Fortran code. Thanks
module WriteResults
use globvar
implicit none
contains
subroutine estimates
implicit none
integer :: t,j,k,s,i
CHARACTER(len=40) :: temp1,temp2
real(8) :: P(ntime,nfac,nfac) !Variance of factor
real(8) :: rho(ntime,nfac,nfac) !Variance of factor
open(1,file='observation.out')
do t = 1, ntime
do j = 1, nequation(t)
k = eqindex(t,j)
write(1,'(2I,<nx+nfac+1>f16.8)')t,k, beta(t,k,:), Z(t,k,:), H(t,k,k)
write(1,'(2I,<nx+nfac+1>f16.8)')t,k,sdbeta(t,k,:),sdZ(t,k,:),sdH(t,k,k)
write(1,*)
end do
write(1,*)
end do
close(1)

  2 Comments

James Tursa
on 16 Jul 2012
Do you mean that you have some MATLAB m-code with some variables, and you want to have m-code that will output results to a file in the same way that the above Fortran code does? Or something else?
Also, please format all of your code above so that it is more readable.
Jan
on 16 Jul 2012
@Jessica: Please do not post 2 question about the same problem. See: http://www.mathworks.com/matlabcentral/answers/43720-error-when-using-writeif-statement . And, as far as I remember I ask the 3rd time, please format your code properly. If you are in doubt, follow the "? help" button, which appears when you create or edit a message. Or read http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup .

Sign in to comment.

1 Answer

Answer by Ben Barrowes on 17 Jul 2012

Jessica,
This is Ben Barrowes. I see you are making progress converting this.
This bracket syntax is meant to be a variable repeat specification as explained here, for example: http://astro.uni-tuebingen.de/software/ftnchek/f77.html
What it means is that the f16.8 should be used nx+nfac+1 times. Sorry f2matlab did not convert this for you. This line in fortran:
write(1,'(2I,<nx+nfac+1>f16.8)')t,k, beta(t,k,:), Z(t,k,:), H(t,k,k)
Should have been converted by f2matlab to be:
writef(fid_1,[repmat('%i',1,2),repmat('%16.8f',1,nx+nfac+1)],t,k, beta(t,k,:), z(t,k,:), h(t,k,k));
The other lines with the bracket notation would be similar. Thanks for catching this.
bb

  2 Comments

Star Strider
on 17 Jul 2012
I can't find any documentation for ‘writef’ in MATLAB. I believe the correct current MATLAB function is ‘fprintf’.
Right, writef is a function supplied by f2matlab when it converts the fortran to matlab code.

Sign in to comment.