在 Matlab 中读取和写入二进制文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/205735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:40:17  来源:igfitidea点击:

Read and write from/to a binary file in Matlab

matlabfile-iobinary

提问by AnnaR

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

我对 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)?

我有一个包含以二进制格式存储的数据(16 位整数)的文件。如何在matlab中将其读入向量/数组?如何将此数据写入matlab中的文件?在读取/写入大量数据(千兆字节)时,是否有任何智能调整来提高性能速度?

回答by Azim

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

正如蜥蜴比尔所写,您可以使用 fread 将数据加载到向量中。我只想稍微扩展一下他的回答。

Reading Data

读取数据

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

The commands fopenand freaddefault to Little-endian[1] encoding for the integers. If your file is Big-endian encoded you will need to change the freadto

命令fopenfread默认为整数的 Little-endian[1] 编码。如果您的文件是大端编码的,则需要将fread更改为

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

Also, if you want to read the whole file set

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

>> count=inf;

and if you want to read the data into matrix with ncolumns use

如果要将数据读入具有n列的矩阵,请使用

>> count=[n inf];

Writing Data

写入数据

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

至于将数据写入文件。该命令,FWRITE,在比尔的回答将写入到一个二进制文件。如果要将数据写入文本文件,可以使用dlmwrite

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

References

参考

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

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

Update

更新

  1. The machine format (IE, ieee-be, ieee-le, vaxdetc.) of the binary data can be specified in either the fopenor the freadcommands in Matlab. Details of the supported machine format can be found in Matlab's documentation of fopen.

  2. Scott French'scomment to Bill's answersuggests reading the data into an int16 variable. To do this use

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

    where countis the size/shape of the data to be read, precisionis the data format, and machineformatis the encoding of each byte.

  3. See commands fseekto move around the file. For example,

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

    will rewind the file to the beginning where bofstands for beginning of file.

  1. 二进制数据的机器格式(IE、ieee-beieee-levaxd等)可以在 Matlab的fopenfread命令中指定 。支持的机器格式的详细信息可以在 Matlab 的fopen文档中找到。

  2. Scott FrenchBill 回答评论 建议将数据读入 int16 变量。要做到这一点,请使用

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

    其中count是要读取的数据的大小/形状,precision是数据格式,machineformat是每个字节的编码。

  3. 查看命令fseek来移动文件。例如,

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

    将文件倒带到bof代表文件开头的开头

回答by Bill the Lizard

Assuming you know how many values you have stored in the file, you can do something like this to read the data into an array.

假设您知道文件中存储了多少个值,您可以执行类似的操作将数据读入数组。

fid = fopen('data.bin','rb')
A = fread(fid, count, 'int16')

To write data to a file do this:

要将数据写入文件,请执行以下操作:

fid = fopen('data.bin','w')
count = fwrite(fid, A, 'int16')

The fwrite function returns the number of elements(not bytes) written to the file.

fwrite 函数返回写入文件的元素数(不是字节数)。

As far as performance tuning goes, you can read data in chunks to only use as much as you need to process. This is the same in any language, and there's no way to speed it up that's specific to Matlab.

就性能调优而言,您可以分块读取数据,以仅使用您需要处理的数量。这在任何语言中都是一样的,并且没有办法加快它特定于 Matlab 的速度。

回答by Anthony Potts

I usually hate seeing links in a response, but this looks pretty close:

我通常讨厌在响应中看到链接,但这看起来非常接近:

http://www.mathworks.com/support/tech-notes/1400/1403.html

http://www.mathworks.com/support/tech-notes/1400/1403.html

As to the second part of performance tuning, it's been 6 years since I've used Matlab, so I don't know.

关于性能调优的第二部分,我用Matlab已经6年了,不知道。

HTH

HTH