【发布时间】:2011-10-19 13:25:36
【问题描述】:
我想通过matlab读取包含二进制数据的平面文件,,, 我怎样才能做到这一点..? 数据实际上是在 .dat 文件中以二进制形式保存的双重数字
谢谢
【问题讨论】:
标签: c# matlab file-io binaryfiles
我想通过matlab读取包含二进制数据的平面文件,,, 我怎样才能做到这一点..? 数据实际上是在 .dat 文件中以二进制形式保存的双重数字
谢谢
【问题讨论】:
标签: c# matlab file-io binaryfiles
方法很多,我一般用fread
fileId = fopen('mybinaryfile.dat','r'); %# open the file for reading
myData = fread(fileId,Inf,'double'); %# read everything (Inf) in the file as 'double' values
如果您的数据几乎无法放入内存,您可以使用多次读取来访问它
sizeToRead = 10000; %# limit size to 10000 values
fileId = fopen('mybinaryfile.dat','r'); %# open the file for reading
keepGoing=1; %# initialize loop
while(keepGoing)
%# read a maximum of 'sizeToRead' values
myData = fread(fileId,sizeToRead,'double');
%# ...
%# process your data here
%# ...
%# make the loop stop if end of file is reached or error happened
if numel(myData) ~= sizeToRead
keepGoing=0;
end
end
【讨论】:
fseek(fileId,numberOfBytes,'cof')。 'double' 大小为 8 个字节,因此在您的情况下,您需要执行 fseek(fileId,8*123,'cof')
使用 FileStream 打开此文件,然后将其包装到 BinaryReader。它为您提供了 ReadDouble、ReadByte 等方法。
【讨论】: