Fortran to matlab conversion

3 visualizzazioni (ultimi 30 giorni)
Jessica
Jessica il 16 Lug 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 Commenti
James Tursa
James Tursa il 16 Lug 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
Jan il 16 Lug 2012
Modificato: Jan il 16 Lug 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 .

Accedi per commentare.

Risposte (1)

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

Accedi per commentare.

Categorie

Scopri di più su Fortran with MATLAB in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by