【问题标题】:Read flat-file from matlab从 matlab 读取平面文件
【发布时间】:2011-10-19 13:25:36
【问题描述】:

我想通过matlab读取包含二进制数据的平面文件,,, 我怎样才能做到这一点..? 数据实际上是在 .dat 文件中以二进制形式保存的双重数字

谢谢

【问题讨论】:

    标签: c# matlab file-io binaryfiles


    【解决方案1】:

    方法很多,我一般用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
    

    【讨论】:

    • if numel(myData) ~= sizeToRead keepGoing=0; end 这是什么条件?
    • 还有 laurent 先生,如何寻找二进制文件中的特定位置,例如我想将指针(光标)从 0(开始)移动到 123 >>>?
    • 如果读取数据的数量与您要求的不同,则您在文件末尾,并退出循环。
    • 要寻找给定的位置,使用fseek(fileId,numberOfBytes,'cof')。 'double' 大小为 8 个字节,因此在您的情况下,您需要执行 fseek(fileId,8*123,'cof')
    • 什么是(123)? 8 是 double 字节的大小,但 123 是什么?还有 laurent 先生如何获取文件中的当前指针位置:?
    【解决方案2】:

    使用 FileStream 打开此文件,然后将其包装到 BinaryReader。它为您提供了 ReadDouble、ReadByte 等方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-21
      • 2014-04-22
      • 2014-07-20
      相关资源
      最近更新 更多