필터 지우기
필터 지우기

Skipping bytes in binary file

조회 수: 14 (최근 30일)
Tevin
Tevin 2022년 12월 29일
댓글: Walter Roberson 2022년 12월 30일
I have a binary file with the following values. The first 4 are each 2 bytes and double precision. The following are pairs of values of double precision. The bytes are unknown for the pair that follows. How can I use fread to skip the first 4 values (8 bytes) and extract the pairs?
1
2
3
4
1 2 4
2 2 5
4 4 4
  댓글 수: 2
Jan
Jan 2022년 12월 29일
"The first 4 are each 2 bytes and double precision." - Double precision means 8 bytes per value.
"The following are pairs of values of double precision." - Which pairs? I see triples.
Tevin
Tevin 2022년 12월 29일
Oh sorry. The following are triples and I am trying to extract those.

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

채택된 답변

Voss
Voss 2022년 12월 29일
You can just fread the appropriate number of bytes (i.e., the amount you want to skip). For example:
% read 4 double-precision numbers (32 bytes), and don't store the result
fread(fid,4,'double');
Or you can use fseek to move the file position indicator forward by the appropriate amount:
% move 32 bytes forward, effectively skipping those bytes
fseek(fid,32,'cof')
The next fread call reads from where fseek placed the file position indicator.
  댓글 수: 2
Tevin
Tevin 2022년 12월 29일
Thank you. I am still not able to extract those triples.
Walter Roberson
Walter Roberson 2022년 12월 30일
triples = fread(fid, [3 NUMBER_OF_TRIPLES], 'uint8=>double') .';
Use inf for NUMBER_OF_TRIPLES if you want to read to end of file.
Each row of triples will be 3 entries, converted to double precision already -- ready for you to do calculations such as
triples(:,2) * 2^16 + triples(:,1) * 2^8 + triples(:,3)
or as appropriate depending what byte order is used for the triples.

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

추가 답변 (0개)

카테고리

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

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by