how to read a large binary file(*.dat file format) and store it in another file?
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
Geoff Hayes
2014-9-3
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 个评论
Geoff Hayes
2014-9-4
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?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!