LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
LinkBack Search this Thread
Old 07-02-2009, 05:39 PM   #1
BrandonPossible
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
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.
 
Old 07-02-2009, 06:22 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 11,534

Rep: Reputation: 674Reputation: 674Reputation: 674Reputation: 674Reputation: 674Reputation: 674
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
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,361

Rep: Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909
Quote:
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:
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
 
  


Reply


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
Trackbacks are Off
Pingbacks are On
Refbacks are 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 10:50 PM.

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