how to read a large binary file(*.dat file format) and store it in another file?

4 views (last 30 days)
Hello, I have a binary data in a *.dat file format (i.e temp.dat). Its a 4DVar data (i.e lat.,lon.,pressure and temp). how to read these data files in MatLab and store it in another file? I cannot attach these files here as it shows that files are unsupported. Please, help me to get the values of temperature from these file formats. If there is any toolbox to read these files, kindly help me regarding this. I have MatLab version "R2013a".
Thanks & regards

Answers (1)

Geoff Hayes
Geoff Hayes on 3 Sep 2014
Girija - do you have four files, one for latitude, longitude, pressure and temperature? Or are all four pieces of information stored within the same file? Do you know how many (say) temperature values there are?
Since the data is in binary, you must know the data type of each element (integer, double, float, etc.) so check out using fopen to open the file, and fread to read the data in.
For example (and this is taken from the link to fread)
% open the file for writing
fid = fopen('temp.dat','wb');
if fid>0
% create a 5x5 matrix
A = magic(5);
% write the matrix data to file
fwrite(fid,A,'double');
% close the file
fclose(fid);
end
% open the file for reading
fid = fopen('temp.dat','rb');
if fid>0
% continue reading until end-of-file is reached
while ~feof(fid)
% read a double
dblVal = fread(fid,1,'double');
% do something with dblVal
if ~isempty(dblVal)
fprintf('dblVal = %f\n',dblVal);
end
end
% close the file
fclose(fid);
end
You can do more than just read in one element at a time (which can be slow) and so can read in multiple elements instead. See the documentation for fread for more details.
In the above code, we write double data to file, and so must read back in the same data type. You will have to do the same for your code given the data type of that data within the binary files.
  7 Comments
Geoff Hayes
Geoff Hayes on 4 Sep 2014
Which website, because it might provide details on how the data files are constructed?
Do you have to download software to create the data, or do you run it from the web?

Sign in to comment.

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!