LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   3D array in MAT file ->DAT file to read in FORTRAN (https://www.linuxquestions.org/questions/programming-9/3d-array-in-mat-file-dat-file-to-read-in-fortran-737317/)

BrandonPossible 07-02-2009 05:39 PM

3D array in MAT file ->DAT file to read in FORTRAN
 
Alright, so I have been trying to resolve this issue for awhile, but now feel like help is very necessary.

I have a 128(by)128(by)128 array in a MAT file, and am using the following MATLAB script to convert it to a DAT file:



load C_42.mat
fid=fopen('C_42.dat','wt')
fwrite(fid,Nu)
fclose(fid)




With it in the DAT format I am hoping to read it with a FORTRAN code, at which point I will be able to manipulate the array values and output it to a binary file.

Below is the read command in the FORTRAN code:



fn1='/home/C_42.dat'
open(11,file=fn1,form='FORMATTED')
read(11,'(4ES10.9)') u1,v1,w1
close(11)


Currently, I am getting a runtime error: Bad value during floating point read

(I am using gfortran)

I guess I am mainly lost on formatting, should I have used some sort of formatting in the MATLAB script in order to match the format in the FORTRAN code?

(When I open the MAT file in MATLAB it loads in the workspace as just a 3D matrix, Nu)

Thanks ahead for any possible help.

-B.P.

michaelk 07-02-2009 06:22 PM

Welcome to LinuxQuestions.

A DAT file is a generic term which really does not define a file format. Just because you write to a file called something.dat does not mean it will automatically be converted. I'm not a MatLab script person but what you posted is only coping the file and it is still in a mat format. You will have to use an export function i.e. to CSV for example and then your FORTRAN program will have to read and process the data accordingly.

colucix 07-03-2009 01:14 AM

Quote:

Originally Posted by BrandonPossible (Post 3594859)
With it in the DAT format I am hoping to read it with a FORTRAN code, at which point I will be able to manipulate the array values and output it to a binary file.

But fopen already write a binary file!!! If you want an ascii file you have to use fprintf. But you really don't need Fortran to manipulate data and re-write a file, if you can do that in Matlab.

Anyway, the only thing you have to pay attention is to create a binary file with access "direct" or "sequential". Your code creates the first one. In that case you have to specify access=direct in the open statement in Fortran. If you want to create a binary with sequential access, you have to add a signed 32-bit integer at the beginning and at the end of each record, containing the number of bytes to read. For example:
Code:

>> nbytes = 128*128*128*4;
>> fid = fopen('C_42.dat','w');
>> fwrite(fid, nbytes, 'int32');
>> fwrite(fid, Nu, 'single');
>> fwrite(fid, nbytes, 'int32');
>> fclose(fid);

After that you can read the file in Fortran by this simple code:
Code:

program test
  real, dimension(128,128,128) :: Nu
  open(11, file = 'C_42.dat', form = 'unformatted')
  read(11) Nu
  close(11)
end program test



All times are GMT -5. The time now is 08:05 AM.