【问题标题】:Reading multiple precision binary files through fread in MatlabMatlab中通过fread读取多个精度二进制文件
【发布时间】:2011-12-27 03:46:28
【问题描述】:

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

【问题讨论】:

    标签: matlab file-io binary binaryfiles


    【解决方案1】:

    您可以使用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);
    

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

    【讨论】:

    • @shunyo:很高兴我能帮上忙。在性能方面,您是否将此与使用 memmapfile 的解决方案进行了比较?
    • 是的,我做到了。它在速度方面提高了 5 倍。一个非常方便的解决方案,您的。
    猜你喜欢
    • 2020-06-09
    • 1970-01-01
    • 2015-08-22
    • 2019-04-13
    • 1970-01-01
    • 2019-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多