Fortran to matlab conversion

조회 수: 4 (최근 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