How do i read binary file
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천

Hi guys,
I have to read from a binary file and transform to decimal for example the first 8 groups at 8 bit level and the folowing 7 groups at 56 bit level as integer.
I tried this :
fileID = fopen('test.bin');
precision = '1*uint8';
B = fread(fileID,[1,1],precision);
%% I know this read only the first group and its mean 0 but for example I want to skip the first 8 groups and read after them the following 7 at 56 bit level
%% And after that I want to read specific groups at the same 56 bit level
%% I think I made my self clear otherwise please ask me if something that you dont understand.
Thanks!
채택된 답변
Ameer Hamza
2020년 10월 19일
0 개 추천
Try this
fileID = fopen('test.bin');
precision = '1*uint8';
fseek(fileID, 8, 0); % skip 8 bytes
B = fread(fileID,[1,7],precision); % read 7 bytes
댓글 수: 11
Cristian Martin
2020년 10월 19일
fseek(fileID, 8, 0);
So this line skip the first 8 bytes, ok?
%% But for me to read 56 bits I need to modify the precision ?
precision = '7*ubit56'; % like this?
Ameer Hamza
2020년 10월 19일
You can just use
precision = '*ubit56';
fread(fileID,1,precision)
7*ubit56 is only needed if you are going to use the skip argument to fread().
Cristian Martin
2020년 10월 19일
fileID = fopen('test.bin');
precision = '*ubit56';
fseek(fileID, 8, 0); % skip 8 bytes
A = fread(fileID,1,precision);
This works fine, but now I want to search more multiple 56 bit in the bin file and read all, all I now is that the after this 7 group of 56 bit i have to skip always another 8 gropus of 8 bit and again read the data in 56 bit.
How can I write this ?
Thanks!
Ameer Hamza
2020년 10월 19일
Once the initial 8 bytes are skipped, you can use the skip argument of fread() to skip a blocks of 8 bytes after reading every seven bytes
f = fopen('test.bin', 'r');
precision = '*ubit56';
fseek(f, 8, 0); % skip 8 bytes
A = fread(f,inf,precision, 8*8) % skips is in number of bits
fclose(f);
A is an array and it 'inf' aregument tells fread() to read till end of the file.
Cristian Martin
2020년 10월 19일
Works perfect, Thanks Hamza!
Ameer Hamza
2020년 10월 19일
I am glad to be of help!
Cristian Martin
2020년 10월 20일
One more think Amza, do you think it's possible to run the script continuously, because my data in the file are always updated? I'd figure to use run command but I don't know how to apply a continuous mode which would offer in a different window the results. For that I will use probably output command.
Ameer Hamza
2020년 10월 20일
Can you exaplain what do you mean by continuously running the script. Do you want to run it at regular intervals?
Cristian Martin
2020년 10월 20일
Yes, for example I want to gather data from a file which data's are updated continuasly, let's say I want to run the above code for 20sec after that a pause of 5sec and runnit again, but would be preferable the code to run from the stopped point and not to run from the beginning of the file.
Ameer Hamza
2020년 10월 20일
I haven't experimented with reading the file, which is also being updated. You need to do some tests whether it works correctly and next fread() will read the new data correctly. For periodic execution of the code, check timers.
Cristian Martin
2020년 10월 20일
It's working I already tried and it's OK, the result is updated every time I hit Run. But I guess I have Tu figure how is with the timers... Thanks...
추가 답변 (0개)
카테고리
도움말 센터 및 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!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
