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

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

问题描述

我有与多个precision为{'双','双师型记录一个巨大的二进制文件,
的Int32,INT8','字符'}。我已经使用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 读取记录中的一个列在-A-时间:

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

这code应该适用于任何一般二进制记录文件,你只需要指定记录字段的数据类型和字节长度。结果将在含有列的单元阵列被返回。

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读取多个precision二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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