Reading a binary data file

Hello,
I am trying to import and read a binary data file of the following format in MATLAB. I need to read in all the data, however at this point im just trying to read in the first 4 char, but i keep getting a blank array. Nothing seems to be working and I have been stuck for days.
Thanks in advance for your help!
fid = fopen('file.dat','r');
if fid <0
error('error opening file %s\n file.dat')
end
Program_label = fread(fid,[1 4],*char)
fclose(fid);
m = memmapfile('GSS1_t023_is_phcorr_ideal.dat');
m.Format

Réponses (1)

Jan
Jan le 7 Juin 2019

0 votes

fid = fopen('file.dat','r');
if fid <0
error('error opening file %s\n file.dat')
end
Program_label = fread(fid, [1 4], '*char'); % With quotes
Program_version = fread(fid, 1, 'single');
Type_label = fread(fid, [1 4], '*char');
nCols = fread(fid, 1, 'uint32');
nRows = fread(fid, 1, 'uint32');
...
fclose(fid);

8 commentaires

KatherineS
KatherineS le 8 Juin 2019
Thank you for your answer, however the output for the values just shows as [] rather than the actual data, is there a way to fix this?
Jan
Jan le 8 Juin 2019
Modifié(e) : Jan le 8 Juin 2019
Please explain exactly, what you are doing. How do you obatin the "[]"? "The output for the values shows..." is not clear enough to understand this. Program_label is read as char vector, such that it is unlikely, that the empty matrix [] is shown anywhere. Therefore I assume, that your method to display the contents of the variables is flawed, or that the file really contains '[] ' as first 4 characters.
KatherineS
KatherineS le 8 Juin 2019
Running the code you provided on the file, I obtain what is shown in the image in my workspace. the first characters in the file should be imageLayerStack (abcdef1 v 1.14) Screen Shot 2019-06-08 at 15.31.09.png
Please show us
double(Program_label)
I suspect that it contains unprintable characters, such as binary 0.
the first characters in the file should be imageLayerStack (abcdef1 v 1.14)
That disagrees with the layout you provided, which says that there should be 4 characters followed by a single precision float
Jan
Jan le 8 Juin 2019
Show us:
fid = fopen('file.dat','r');
if fid <0
error('error opening file %s\n file.dat')
end
data = fread(fid, [1 16], '*uint8')
fclose(fid)
KatherineS
KatherineS le 8 Juin 2019
Hello,
The data variable shows as (in image) and double(Program_label) shows as []Screen Shot 2019-06-08 at 22.03.47.png
Thank you all very much for your help in this.
Walter Roberson
Walter Roberson le 8 Juin 2019
The file is empty.
dinfo = dir('file.dat')
and look at the bytes information. I think it will show 0

Connectez-vous pour commenter.

Catégories

En savoir plus sur Large Files and Big Data dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by