Clear Filters
Clear Filters

Fortran to matlab conversion

5 views (last 30 days)
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
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
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)
end do
end do
James Tursa
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
Edited: Jan on 16 Jul 2012
@Jessica: Please do not post 2 question about the same problem. See: . 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 .

Sign in to comment.

Answers (1)

Ben Barrowes
Ben Barrowes on 17 Jul 2012
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:
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.
Star Strider
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’.
Ben Barrowes
Ben Barrowes on 17 Jul 2012
Right, writef is a function supplied by f2matlab when it converts the fortran to matlab code.

Sign in to comment.


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!