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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
07-02-2009, 05:39 PM
|
#1
|
|
LQ Newbie
Registered: Jul 2009
Posts: 1
Rep:
|
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.
|
|
|
|
07-02-2009, 06:22 PM
|
#2
|
|
Moderator
Registered: Aug 2002
Posts: 10,756
|
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.
|
|
|
|
07-03-2009, 01:14 AM
|
#3
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,899
|
Quote:
Originally Posted by BrandonPossible
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
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 05:31 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|