필터 지우기
필터 지우기

Parse a binary file with different number formats

조회 수: 2 (최근 30일)
Antares
Antares 2017년 12월 14일
댓글: Antares 2017년 12월 14일
Hi, I have a binary file representing a surface, there is a header which provides some info about the surface like sampling rates along x and y axis, number of rows and columns and so on... then there is a series of values which represent values along z axis.
The header is made of short, long and float number formats while z values are in floats. An example can be this:
\\ HEADER:
-float (type of figure)
-long (number of rows)
-long (number of columns)
-float (x sampling rate)
-float (y sampling rate)
-float (x origin)
-float (y origin)
...... //end of header
float z1
float z2
float z3
and so on
How can I retrieve all the values scanning the file? I tried with fread but I don't know which format option choose
Thanks

채택된 답변

Jan
Jan 2017년 12월 14일
편집: Jan 2017년 12월 14일
The documentation explains this clearly: doc fread
fid = fopen(File);
if fid == -1, error('Cannot open file %s', File); end
typeOfFigure = fread(fid, 1, 'float'); % -float (type of figure)
numOfRows = fread(fid, 1, 'long'); % -long (number of rows)
numOfCols = fread(fid, 1, 'long'); % -long (number of columns)
xSampleRate = fread(fid, 1, 'float'); % -float (x sampling rate)
ySampleRate = fread(fid, 1, 'float'); % -float (y sampling rate)
xOrigin = fread(fid, 1, 'float); % -float (x origin)
yOrigin = fread(fid, 1, 'float); % -float (y origin)
...
fclose(fid);
Looks trivial, doesn't it?
I'd prefer 'int32' instead of 'long', 'int16' instead of 'short' and 'single' instead of 'float', but this is only for clarity.
  댓글 수: 1
Antares
Antares 2017년 12월 14일
Wow! I didn't expect to be so simple
Thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by