Reading Fortran binary file in MATLAB
2 views (last 30 days)
Show older comments
I am having trouble understanding Binary file and I am trying to figure out a way to read in MATLAB. I just have the following details about the file.
File name = ZDxxx, where xxx = analysis number. File type = binary direct access. Record length = 12 bytes (3 REAL*4).
Item 1 = current drift value. Item 2 = maximum positive value of this drift to this point Item 3 = maximum negative value.
The file contains NTIM+1 sets of NDRFT records, where NTIM = no. of time or load steps (set 1 = state at start of analysis) and NDRFT = no. of drifts.
I have used MATLAB for pretty basic purposes like reading text files and doing some matrix analysis. I am not a computer science student.
2 Comments
dpb
on 7 Jul 2016
Edited: dpb
on 7 Jul 2016
A Fortran direct access file has some additional structure hidden elements in it besides just the data. How that is encoded is not specified by the Fortran Standard; only that a file written by a given compiler can be read by the same compiler.
Typically, there's a record length before and after each record, often 32-bit integer but that's not necessarily so.
The simplest route for reading the data by other than Fortran would be to write it in either a text form or as 'stream' binary file which is now supported by Fortran Standard altho every compiler I'm aware of has had non-standard way of creating such for quite a long time.
You can try something like
fid=fopen('ZD...ext','r');
n=fread(fid,1,'int')
A=fread(fid,3,'single')
n=fread(fid,1,'int')
and see what you get. If you can recognize the first three values in A, then the surmise on the record marker is correct; if not, report back on what
frewind(fid)
R=fread(fid,32,'char*1');
fprintf([repmat('%02X',1,32) '\n'],R)
returns. This will show us the actual content of the first 32 bytes in the file which can use to try to decipher the record encoding.
PS: Check the documentation for your compiler; it may have the description altho it's not guaranteed to be documented since with Fortran-only solution, there's no need for such knowledge...
Martin Zuniga
on 24 Mar 2020
You are trying to read a Perform3D results file, right? I have worked with Perform3D for two years and was looking into reading the binary files directly, did you succeed?
Answers (0)
See Also
Categories
Find more on Fortran with MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!