how to read a large binary file(*.dat file format) and store it in another file?
조회 수: 3 (최근 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에서 Low-Level File I/O에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!