Fortran to matlab conversion

6 次查看(过去 30 天)
Jessica
Jessica 2012-7-16
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 个评论
James Tursa
James Tursa 2012-7-16
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
Jan 2012-7-16
编辑:Jan 2012-7-16
@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 .

请先登录,再进行评论。

回答(1 个)

Ben Barrowes
Ben Barrowes 2012-7-17
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 个评论
Star Strider
Star Strider 2012-7-17
I can't find any documentation for ‘writef’ in MATLAB. I believe the correct current MATLAB function is ‘fprintf’.
Ben Barrowes
Ben Barrowes 2012-7-17
Right, writef is a function supplied by f2matlab when it converts the fortran to matlab code.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Fortran with MATLAB 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by