2015-02-24 120 views
1

我正在嘗試將一些二維座標寫入二進制文件。但是,我從已寫入的文件中讀取的內容與原始數據完全不同。詳情在這裏給出。Matlab中的二進制文件寫入/讀取操作

例如,我有45(X,Y)個點。模擬需要將每個字節存儲爲兩個字節(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()無法獲得我寫入二進制文件的內容。有人幫忙嗎?謝謝。

+0

它是如何失敗?沒有數據?錯誤的數據?錯誤? – excaza 2015-02-24 16:08:56

+0

正在讀/寫同一臺機器(架構)?你可能有一個endian - ness問題。 – 2015-02-24 17:36:38

+0

@excaza錯誤的數據與我寫入文件(原始數據)的內容進行比較。 – user18441 2015-02-25 10:02:41

回答

0

問題可能是(至少部分)是一個endian問題。 在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並從那裏開始工作。