在Matlab中通过fread读取多精度二进制文件

在Matlab中通过fread读取多精度二进制文件,matlab,file-io,binary,binaryfiles,Matlab,File Io,Binary,Binaryfiles,我有一个巨大的二进制文件,它有多个精度为{'Double','Double'的记录, 'Int32','Int8','Char'}。我使用memmapfile读取数据,但是读取数据的速度非常慢。有没有办法通过fread读取整个文件?您可以使用函数的'skip'选项,也可以一次读取一列记录: %# type and size in byte of the record fields recordType = {'double' 'double' 'int32' 'int8' 'char'}; re

我有一个巨大的二进制文件,它有多个精度为{'Double','Double'的记录,
'Int32','Int8','Char'}。我使用memmapfile读取数据,但是读取数据的速度非常慢。有没有办法通过fread读取整个文件?

您可以使用函数的
'skip'
选项,也可以一次读取一列记录:

%# 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);

这段代码通常适用于任何二进制记录文件,您只需指定记录字段的数据类型和字节长度。结果将在包含列的单元格数组中返回。

@shunyo:很高兴我能提供帮助。您是否将此与使用
memmapfile
的解决方案在性能方面进行了比较?是的。它的速度提高了5倍。一个非常方便的解决方案,你的。