How to read a single precision binary data file in to MATLAB?

3 Ansichten (letzte 30 Tage)
I have a data file with the name 'Uall.dat'. It is a binary file with four columns and 3225313 rows [3225313,4]. All the data in the file is in single precision FP32.
I want to read this data in to MATLAB and store it into an array 'A' of same dimensions [3225313,4].
I tried using 'fread' command but that dosent seem to work properly. The data being read seem to have different number of elements in it than the original data.
Original data: 3225313 × 4 = 12901252
Data read in to MATLAB (through the code shown at the bottom): 49654701
What is the solution?
The original data was written using Fortran90 as follows:
! Fortran code to write the data file (in binary)
open (dens, form='unformatted', ACCESS="STREAM", file='Uall.bin')
DO i = 1,Ntotal
write(dens) (REAL(Uall(i,j)), j= 1,Ns)
ENDDO
close(dens)
Matlab code to read the data file.
% Matlab code to read the data file
f = fopen('Uall.bin', 'rb');
D = fread(f, inf, 'float');
fclose(f);
  3 Kommentare
Image Analyst
Image Analyst am 5 Nov. 2022
Dropbox is blocked by my company. And 12 MB is too big to attach here. Can you put the file in your "MATLAB Drive"?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Jan
Jan am 5 Nov. 2022
The posted file has 198'618'806 bytes. This is not a multiple of 4, so it cannot contain only float values.
I guess, that you are not reading the file, which is created by the FORTRAN code, but another file with the same name.
It is suspicious, that your file identifier has the value 18. Maybe there are a lot of formerly opened files. Then close them at first by fclose('all') or restart Matlab.
  3 Kommentare
Jan
Jan am 5 Nov. 2022
I do not see, how the shown code to write the file in FORTRAN can create a file, which does not have a multiple of 4 bytes. But this is not on-topic in this Matlab forum.
Walter Roberson
Walter Roberson am 6 Nov. 2022
Bearbeitet: Walter Roberson am 6 Nov. 2022
I was wondering whether UNFORMATTED STREAM wrote record markers, but when I examine https://fortranwiki.org/fortran/show/Stream+Input+Output I see that they are specific about it being byte-oriented with no markers, so indeed an odd number of bytes should not happen with that program.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Fortran with MATLAB finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by