How do i read binary file
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示

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 日
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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
