Matlab中通过fread读取多个精度二进制文件 [英] Reading multiple precision binary files through fread in Matlab

查看:30
本文介绍了Matlab中通过fread读取多个精度二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a huge binary file which has records with multiple precision as {'Double','Double', 'Int32','Int8','Char'}. I have used memmapfile to read in the data but its painfully slow to read in the data. Is there a way to read the whole file through fread?

推荐答案

您可以使用 FREAD 函数以及 FSEEK 一次读取一列"的记录:

You can use the 'skip' option of the FREAD function as well as FSEEK to read the records one "column" at-a-time:

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

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

This code should work for any binary records file in general, you just have to specify the data types and byte length of the records fields. The result will be returned in a cell array containing the columns.

这篇关于Matlab中通过fread读取多个精度二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆