use fread to read a binary file with multiple data sizes

2 ビュー (過去 30 日間)
Robert
Robert 2011 年 6 月 28 日
I'm trying to read a binary data file that has 4 data points that have 8, 8, 16, and 32 bits of information. Is there a way to read this with fread or another function? I don't know how the file was created but the documentation states this is the file format. Thanks

採用された回答

Walter Roberson
Walter Roberson 2011 年 6 月 28 日
Guessing at the representation:
d1 = fread(fid, 1, '*uint8');
d2 = fread(fid, 1, '*uint8');
d3 = fread(fid, 1, '*uint16');
d4 = fread(fid, 1, '*uint32');
This is not certain, as there can be differences in byte ordering, and differences in signed vs unsigned. Also we could reasonably speculate that the 32 bit piece of information might perhaps represent a single-precision floating point number instead of an integer.

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2011 年 6 月 28 日
If the file had repeated patterns of the above form, then probably the best way to handle it would be to use memmapfile .
Note in particular Example 5 there:
m = memmapfile('records.dat', ...
'Format', { ...
'int16' [2 2] 'model'; ...
'uint32' [1 1] 'serialno'; ...
'single' [1 3] 'expenses'}, ...
'Repeat', 1000);
That establishes a record with a mixed format.
  2 件のコメント
Jan
Jan 2011 年 6 月 28 日
I've tried to read the data sectiopn of C3D files with MEMMAPFILE: e.g. 2000 blocks of alternating 4*100 and 32*9 SINGLEs. Because these blocks needed to be transposed before concatenating, the MEMMAPFILE approach was 5 times slower than pre-allocation + FREAD.
Robert
Robert 2011 年 6 月 29 日
The data file is 57000x273 so I'm using something like this:
for i = 1:57000
A(i,1) = fread(fid, 1, '*uint8');
A(i,2) = fread(fid, 1, '*uint8');
A(i,3) = fread(fid, 1, '*uint16');
A(i,4:273) = fread(fid, 270, '*float');
end
What is a more efficient way of handling this?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLarge Files and Big Data についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by