poster
2019-12-07, 23:17
我对matlab的了解只是在需要了解的基础上,因此这可能是一个基本问题。尽管如此,它来了:
我有一个包含以二进制格式存储的数据(16位整数)的文件。如何在Matlab中将其读入向量/数组?如何将此数据写入matlab中的文件?读写大量数据(千兆字节)时,是否有任何智能调整可提高性能速度?
回答:
正如蜥蜴人Bill (https://stackoverflow.com/questions/205735/read-and-write-fromto-a-binary-file-in-matlab#205819)所写,您可以使用fread将数据加载到向量中。我只想扩大他的答案。
读取数据
>> 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缺省为整数的Little-endian [1]编码。如果您的文件是Big-endian编码的,则需要将fread更改为
>> A = fread(fid, count, 'int16', 'ieee-be'); 另外,如果您想读取整个文件集
>> count=inf; 如果您想将数据读入n列矩阵
>> count=[n inf]; 写数据
至于将数据写入文件。 Bill的 (https://stackoverflow.com/questions/205735/read-and-write-fromto-a-binary-file-in-matlab#205819)答案中的命令fwrite将写入二进制文件。如果要将数据写入文本文件,可以使用dlmwrite
>> dlmwrite('data.csv',A,','); 参考文献
[1] http://en.wikipedia.org/wiki/Endianness
更新资料
可以在Matlab的fopen或fread命令中指定二进制数据的机器格式(即ieee-be , ieee-le , vaxd等)。可以在Matlab的fopen文档中找到支持的机器格式的详细信息。
Scott French (https://stackoverflow.com/users/4928/scott-french)对Bill的回答 (https://stackoverflow.com/questions/205735/read-and-write-fromto-a-binary-file-in-matlab#205819) 的 (https://stackoverflow.com/users/4928/scott-french)评论建议将数据读入int16变量。为此使用
>> A = int16(fread(fid,count,precision,machineFormat)); 其中count是要读取的数据的大小/形状, precision是数据格式,而Machineformat是每个字节的编码。
请参阅命令fseek来移动文件。例如,
>> fseek(fid,0,'bof'); 会将文件倒带到bof代表文件开头的开头 。
我有一个包含以二进制格式存储的数据(16位整数)的文件。如何在Matlab中将其读入向量/数组?如何将此数据写入matlab中的文件?读写大量数据(千兆字节)时,是否有任何智能调整可提高性能速度?
回答:
正如蜥蜴人Bill (https://stackoverflow.com/questions/205735/read-and-write-fromto-a-binary-file-in-matlab#205819)所写,您可以使用fread将数据加载到向量中。我只想扩大他的答案。
读取数据
>> 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缺省为整数的Little-endian [1]编码。如果您的文件是Big-endian编码的,则需要将fread更改为
>> A = fread(fid, count, 'int16', 'ieee-be'); 另外,如果您想读取整个文件集
>> count=inf; 如果您想将数据读入n列矩阵
>> count=[n inf]; 写数据
至于将数据写入文件。 Bill的 (https://stackoverflow.com/questions/205735/read-and-write-fromto-a-binary-file-in-matlab#205819)答案中的命令fwrite将写入二进制文件。如果要将数据写入文本文件,可以使用dlmwrite
>> dlmwrite('data.csv',A,','); 参考文献
[1] http://en.wikipedia.org/wiki/Endianness
更新资料
可以在Matlab的fopen或fread命令中指定二进制数据的机器格式(即ieee-be , ieee-le , vaxd等)。可以在Matlab的fopen文档中找到支持的机器格式的详细信息。
Scott French (https://stackoverflow.com/users/4928/scott-french)对Bill的回答 (https://stackoverflow.com/questions/205735/read-and-write-fromto-a-binary-file-in-matlab#205819) 的 (https://stackoverflow.com/users/4928/scott-french)评论建议将数据读入int16变量。为此使用
>> A = int16(fread(fid,count,precision,machineFormat)); 其中count是要读取的数据的大小/形状, precision是数据格式,而Machineformat是每个字节的编码。
请参阅命令fseek来移动文件。例如,
>> fseek(fid,0,'bof'); 会将文件倒带到bof代表文件开头的开头 。