Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 07-02-2009, 05:39 PM   #1
LQ Newbie
Registered: Jul 2009
Posts: 1

Rep: Reputation: 0
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

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:

read(11,'(4ES10.9)') u1,v1,w1

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.

Old 07-02-2009, 06:22 PM   #2
Registered: Aug 2002
Posts: 13,317

Rep: Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092
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.
Old 07-03-2009, 01:14 AM   #3
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966
Originally Posted by BrandonPossible View Post
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:
 >> 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:
program test
  real, dimension(128,128,128) :: Nu
  open(11, file = 'C_42.dat', form = 'unformatted')
  read(11) Nu
end program test


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script, read file into array (Newbie) TheMeeks Programming 6 04-17-2007 08:20 AM
c++ file string from text file into 1d array ak3 Programming 16 12-29-2006 09:45 AM
script to read file into array STAGED Programming 4 01-27-2006 08:06 PM
how to read a file into an array cyb0rg777 Programming 4 09-23-2005 08:12 AM
renaming or converting a text file to a dat file... tangaz Linux - Software 1 10-24-2003 06:57 AM

All times are GMT -5. The time now is 12:37 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration