shu*_*nyo 6 binary file-io matlab binaryfiles
我有一个巨大的二进制文件,其中包含多个精度的记录{'Double','Double','Int32','Int8','Char'}.我已经使用memmapfile读取数据,但读取数据的速度很慢.有没有办法通过fread读取整个文件?
您可以使用FREAD函数的'skip'选项以及FSEEK来一次读取一个"列"的记录:
%# type and size in byte of the record fields
recordType = {'double' 'double' 'int32' 'int8' 'char'};
recordLen = [8 8 4 1 1];
R = cell(1,numel(recordType));
%# read column-by-column
fid = fopen('file.bin','rb');
for i=1:numel(recordType)
%# seek to the first field of the first record
fseek(fid, sum(recordLen(1:i-1)), 'bof');
%# % read column with specified format, skipping required number of bytes
R{i} = fread(fid, Inf, ['*' recordType{i}], sum(recordLen)-recordLen(i));
end
fclose(fid);
Run Code Online (Sandbox Code Playgroud)
这段代码一般适用于任何二进制记录文件,您只需指定记录字段的数据类型和字节长度即可.结果将在包含列的单元格数组中返回.