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

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 commentaires

Just a note: There is no 'b' permission in Matlab for 25 years anymore. At least, the 'b' does not make troubles, bit it is simply ignored.
Dropbox is blocked by my company. And 12 MB is too big to attach here. Can you put the file in your "MATLAB Drive"?

Connectez-vous pour commenter.

Réponses (1)

Jan
Jan le 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 commentaires

hemanth chandra vamsi kakumani
Modifié(e) : Jan le 5 Nov 2022
I am pretty sure I am reading the correct file.
The fact that the file has 198'618'806 bytes, suggests there is something wrong with the way I am writing the inpuut file itself in Fortran. I am attaching the link to ASCII version of the same file here. I could not find anything fishy, may be you want to have a look too.
You are right about the file identifier. Doing fclose('all') made f=3 now. The fclose('all') command dosent seem to make any difference to the size of the data being read into matlab.
Is there a better way to write the input file from Fortran?
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 le 6 Nov 2022
Modifié(e) : Walter Roberson le 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Fortran with MATLAB dans Centre d'aide et File Exchange

Produits

Version

R2020a

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by