读取及写入/在Matlab中的二进制文件 [英] Read and write from/to a binary file in Matlab

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

问题描述

我的MATLAB的知识仅仅是一个需要了解的基础上,所以这可能是一个基本的问题。然而就到这里吧:

My knowledge of matlab is merely on a need to know basis, so this is probably an elementary question. Nevertheless here it comes:

我有包含二进制格式存储的数据(16位整数)的文件。我如何读入向量/ MATLAB中数组?我怎样写这个数据在MATLAB的文件?有没有什么聪明的好办法读/写一个巨大的数据(千兆)量时提高性能速度?

I have got a file containing data (16-bit integers) stored in binary format. How do I read it into a vector /an array in matlab? How do I write this data to a file in matlab? Is there any smart tweak to increase the performance speed when reading/writing a huge amount of data (gigabytes)?

推荐答案

作为<一个href=\"http://stackoverflow.com/questions/205735/read-and-write-fromto-a-binary-file-in-matlab#205819\">Bill蜥蜴写下您可以使用FREAD将数据加载到一个载体。我只是想扩大他的回答一点。

As Bill the Lizard wrote you can use fread to load the data into a vector. I just want to expand a little on his answer.

>> fid=fopen('data.bin','rb') % opens the file for reading
>> A = fread(fid, count, 'int16') % reads _count_ elements and stores them in A.

命令的fopen FREAD 的默认为小端[1]整数编码。如果你的文件是big-endian的EN codeD,您将需要更改的 FREAD

The commands fopen and fread default to Little-endian[1] encoding for the integers. If your file is Big-endian encoded you will need to change the fread to

>> A = fread(fid, count, 'int16', 'ieee-be');

另外,如果你想阅读整个文件集

Also, if you want to read the whole file set

>> count=inf;

如果你想将数据与读入矩阵的 N 的列使用

>> count=[n inf];

书写数据

至于数据知晓到文件中。该命令的 FWRITE 的,在<一个href=\"http://stackoverflow.com/questions/205735/read-and-write-fromto-a-binary-file-in-matlab#205819\">Bill's答案将写入二进制文件。如果你想将数据写入到一个文本文件,你可以使用的 dlmwrite

Writing Data

As for witting the data to a file. The command, fwrite, in Bill's answer will write to a binary file. If you want to write the data to a text file you can use dlmwrite

>> dlmwrite('data.csv',A,',');

参考

[1] http://en.wikipedia.org/wiki/Endianness


  1. 本机格式(即 IEEE-是的,
    IEEE乐的,二进制数据的 vaxd 的等)可在任一指定的
    的fopen 的或在MATLAB中的 FREAD 的命令。所支持的详细信息
    机格式中可以找到
    的fopen 的Matlab中的文档。

  1. The machine format (IE, ieee-be, ieee-le, vaxd etc.) of the binary data can be specified in either the fopen or the fread commands in Matlab. Details of the supported machine format can be found in Matlab's documentation of fopen.

斯科特法国的注释<一个href=\"http://stackoverflow.com/questions/205735/read-and-write-fromto-a-binary-file-in-matlab#205819\">Bill's
回答
表明读取数据到一个
INT16变量。要做到这一点使用

Scott French's comment to Bill's answer suggests reading the data into an int16 variable. To do this use

>> A = int16(fread(fid,count,precision,machineFormat));

其中的计数的是尺寸/外形
要读取的数据,的 precision 的是
的数据格式,和 machineformat
是每一个字节的编码。

where count is the size/shape of the data to be read, precision is the data format, and machineformat is the encoding of each byte.

请参阅命令 fseek的应用于文件中移动。例如,

See commands fseek to move around the file. For example,

>> fseek(fid,0,'bof');

将文件后退到开始在那里的 BOF 的代表文件的开始

这篇关于读取及写入/在Matlab中的二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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