MATLAB Answers

0

Reading a binary data file

Asked by KatherineS on 7 Jun 2019
Latest activity Commented on by Walter Roberson
on 8 Jun 2019
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

  0 Comments

Sign in to comment.

1 Answer

Answer by Jan
on 7 Jun 2019

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 Comments

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.
The file is empty.
dinfo = dir('file.dat')
and look at the bytes information. I think it will show 0

Sign in to comment.