【问题标题】:binary file write/read operation in MatlabMatlab中的二进制文件写/读操作
【发布时间】:2015-04-26 08:39:25
【问题描述】:

我正在尝试将一些 2D 坐标写入二进制文件。但是,我从已写入的文件中读取的内容与原始数据完全不同。此处提供详细信息。

例如,我有 45 (X,Y) 个点。 X 和 Y 都是小于 600 的整数。模拟要求分别存储两个字节(8 位),每个字节的高 2 位保留(对于 X,保留位由 .mrk 填充,即 1 或2;对于 Y,只需使用 0 代替)。在这种情况下,14位二进制数可以表示最大值,16383。我用几种方式写入数据:

in_tmp 是由点数(.nm)、点保留标记(.mrk)和点坐标(.coor)组成的结构

for i=1:in_tmp.nm
    x1 = dec2bin(in_tmp.coor(i,1));
    y1 = dec2bin(in_tmp.coor(i,2));
      t1 = in_tm.mrk(i);
      if(t1==1)
          t2 = '01';
          t2b = 1;
      elseif(t1==2)
          t2 = '10';
          t2b = 2;
      end
      lenx = 16-length(x1);
      leny = 16-length(y1);
      x1hl = strcat(t2, '00000000000000');   % High and low
      y1hl = '0000000000000000';
      x1a = strcat(x1hl(1:lenx), num2str(x1));
      y1a = strcat(y1hl(1:leny), num2str(y1));
      y1a(1:2) = '00';
%     x1b = in_tmp.coor(i,1);
%     y1b = in_tmp.coor(i,2);
%     fwrite(fp1, t2b, 'ubit2');
%     fwrite(fp1, x1b, 'ubit14');
%     
%     fwrite(fp1, 0, 'ubit2');
%     fwrite(fp1, y1b, 'ubit14');
      fwrite(fp1, bin2dec(x1a), 'uint16');
      fwrite(fp1, bin2dec(y1a), 'uint16');
%     fwrite(fp1, bin2dec(x1a(1:8)), 'uint8');
%     fwrite(fp1, bin2dec(x1a(9:end)), 'uint8');
%     fwrite(fp1, bin2dec(y1a(1:8)), 'uint8');
%     fwrite(fp1, bin2dec(y1a(9:end)), 'uint8');
%     x1c = in_tmp.coor(i,1);
%     y1c = in_tmp.coor(i,2);
%     
%     x1hex = dec2hex(x1c);
%     y1hex = dec2hex(y1c);
%     if(length(x1hex)>2)
%         x1h = x1hex(1:end-2);
%         x1l = x1hex(end-1:end);
%     else
%         x1h = dec2hex(0);
%         x1l = x1hex;
%     end
%     
%     tx1h = dec2bin(hex2dec(x1h));
%     l1 = length(tx1h);
%     bin0 = dec2bin(128);    % '10000000'
%     if(t1==1)
%         bin0(end-l1+1:end) = tx1h;
%         bin0(1)=0;
%         bin0(2)=1;
%         
%     elseif(t1==2)
%         bin0(end-l1+1:end) = tx1h;
%     end
%     x1h = bin2dec(tx1h);
%     
%         if(length(y1hex)>2)
%         y1h = y1hex(1:end-2);
%         y1l = y1hex(end-1:end);
%     else
%         y1h = dec2hex(0);
%         y1l = y1hex;
%     end
%     fwrite(fp1, x1h, 'uint8');
%     fwrite(fp1, hex2dec(x1l), 'uint8');
%     fwrite(fp1, hex2dec(y1h), 'uint8');
%     fwrite(fp1, hex2dec(y1l), 'uint8');
end

我的阅读方式

    for i=1:mt.nm              % nm points.
        mred(i,6) = fread(fp1, 1, 'uint8');       % Raw X coordinates.
        mred(i,7) = fread(fp1, 1, 'uint8');       % upper 2 bits are reserved info.
        tmpx = [dec2bin(mred(i,6)), dec2bin(mred(i,7))];
        if(length(tmpx)==16)
            mred(i,4) = bin2dec(tmpx(1:2));       % Real Mark.
            mred(i,1) = bin2dec(tmpx(3:end));     % Real X.
        elseif(length(tmpx)==15)
            mred(i,4) = bin2dec(tmpx(1));         % Real Type.
            mred(i,1) = bin2dec(tmpx(2:end));     % Real X.
        else
            mred(i,4) = bin2dec(tmpx(1:2));       % Type unknown.
            mred(i,1) = bin2dec(tmpx(3:end));     % Real X.
        end       
          mred(i,8) = fread(fp1, 1, 'uint8');       % Y coordinates.
          mred(i,9) = fread(fp1, 1, 'uint8');       % upper 2 bits are reserved.
          tmpy = [dec2bin(mred(i,8)), dec2bin(mred(i,9))];
          if(length(tmpy)==16)
              mred(i,10) = bin2dec(tmpy(1:2));      % Real reserved.
              mred(i,2) = bin2dec(tmpy(3:end));     % Real Y.
          elseif(length(tmpy)==15)
              mred(i,10) = bin2dec(tmpy(1));        % Real reserved.
              mred(i,2) = bin2dec(tmpy(2:end));     % Real Y.
          else
              mred(i,10) = -1;                      % Reserved unknown.
              mred(i,2) = bin2dec(tmpy);            % Real Y.
          end               
    end

read() 函数适用于通过 C++ 实现的给定软件。该软件以这种格式生成坐标系列。然后,我准备了一个 read() 来获取 C++ 软件生成的二进制文件中的信息。然后,我想用 Matlab 以这种格式实现 write(),但是 read() 无法获取我写入二进制文件的内容。有人帮忙吗?谢谢。

【问题讨论】:

  • 它是如何失败的?没有数据?数据错误?出错了吗?
  • 是在同一台机器上读/写(架构)吗?您可能有字节序问题。
  • @excaza 与我写入文件的内容(原始数据)相比,数据错误。
  • @TryHard 实际上,我实现了读取功能,从软件生成的二进制文件中获取数据。它完美地工作。但是当我读取()我的二进制文件时,它得到了错误的数据。在创建二进制文件时,我已经尝试使用“l”和“b”。但是我的 read() 中打开二进制文件的参数是默认的(不指定'l'或'b')。

标签: matlab file binary read-write


【解决方案1】:

问题可能(至少部分)是字节序问题。 在 intel 架构(小端)上,读取两个字节为 uint8,然后附加二进制表示通常不会给您与读取两个字节为 uint16 相同的结果。

以下脚本说明了交换两个 uint8 的顺序将如何解决问题。

我更改了一些变量名称,并使内容更简洁以提高可读性。

% declare an input int and the header (in binary string rep)

v = 68;
t2 = '01';

% write to file

x1 = dec2bin(v);
lenx = 16-length(x1);
x1hl = strcat(t2, '00000000000000');   % High and low
x1a = strcat(x1hl(1:lenx), num2str(x1));

% x1a = 0100000001000100

fp1=fopen('temp','w+');
fwrite(fp1, bin2dec(x1a), 'uint16');

% read the file

frewind(fp1);
vtest = fread(fp1, 1, 'uint16');       % upper 2 bits are reserved info.
dec2bin(vtest)

% ans = 100000001000100

% now read *two* bytes as two uint8

frewind(fp1);

byte1 = fread(fp1, 1, 'uint8');       % Raw X coordinates.
byte2 = fread(fp1, 1, 'uint8');       % upper 2 bits are reserved info.
tmpx = [dec2bin(byte2) dec2bin(byte1)]; % <-- swap the order

% tmpx = 10000001000100

或者,只需将整个 2 个字节读取为 uint16 并从那里开始工作。

【讨论】:

    猜你喜欢
    • 2010-09-17
    • 1970-01-01
    • 1970-01-01
    • 2012-01-26
    • 2018-07-26
    • 2016-06-14
    • 2021-06-03
    • 1970-01-01
    相关资源
    最近更新 更多