2010-11-30 214 views
1

我想讀取有十六進制數據的txt文件。我想用十進制數來轉換它們,除了一列我想轉換成二進制位並將它們寫入8個單獨的列中。 樣本數據集十六進制爲二進制和十進制在matlab中

1/4/2010 15時31 < 00000> 0×0001 0×0010 0×0000 0x0014 0x0001的0x0142 0x0001的0×0000 0x028F 0x2007 0x0105 0x00AA 0x005A 0xFA8C 0xFACD 0xFAED 0x003B 0xFFA3 0xFFDE 0x0080 0xFEE0 0xFF2E爲0x0000 0x0108 1/4/2010 15點31 < 00000> 0×0001 0×0010 0×0000 0x0014 0x0001的0x0143 0x0001的0×0000 0x028F 0x2008 0x0105 0x00AA 0x005B 0xFA8C 0xFACC 0xFAEE 0x003C 0xFFA3 0xFFDE 0x0080 0xFEE0 0xFF2E爲0x0000 0x0108 1/4/2010 15時31 < 00000> 0×0001 0×0010 0×0000 0x0014 0x0001的0x0144 0x0001的0x0000 0x028F 0x2009 0x0105 0x00A9 0x005C 0xFA8C 0xFACC 0xFAF0 0x003B 0xFFA3

清除所有; %

並[b,pathb] = uigetfile({ '* TXT。'}, '選擇文件', 'C:\數據\ 2010');

file2 = [pathb b]; data = dlmread('file2','\ t',2,1);

newdata = hex2dec(data);

現在我不知道如何擺脫所有值中的0x,我需要將最後一列轉換爲二進制並寫入8列。

任何幫助,高度讚賞。 感謝

回答

1

這裏有一個稍微不同的策略,你可以嘗試使用TEXTSCAN先閱讀所有的數據字符串:

fid = fopen(file2,'rt');     %# Open the file 
str = ['%s %s %s ' repmat('0x%s ',1,24)]; %# Format string for columns 
C = textscan(fid,str,'CollectOutput',1); %# Read all fields as strings 
              %# (removing 0x's) 
fclose(fid);        %# Close the file 
C = C{1};         %# Remove outer cell encapsulation 

dates = strcat(C(:,1),{' '},C(:,2));   %# Collect the date strings 
decValues = cellfun(@hex2dec,C(:,4:end-1)); %# Convert the first 23 columns to 
              %# decimal values 
decValues = decValues-65536.*(decValues > 32767); %# Change from unsigned to 
                %# signed 16 bit values 
binValues = cellfun(@(n) dec2bin(hex2dec(n),8),... %# Convert the last column 
        C(:,end),'UniformOutput',false); %# to binary strings 

如果你在你的文件有N行,你應該結束了:

  • 日期字符串(可以轉換爲serial date numbersdate vectors)的N×1單元陣列dates
  • 包含轉換的十進制值的一個N乘23的數組decValues。使用two's complement將值從範圍0到65535(即無符號的16位整數)轉換爲-32768到32767(即有符號的16位整數)。
  • 包含轉換的二進制值的N×1單元陣列binValues。每個單元格包含一個1乘8字符的零和一個字符串。
+0

我不斷收到此錯誤.... [錯誤在==> hex2decimal在6 C = textscan(fid,repmat({'%s'},27)); %#以字符串形式讀取數據]還有一個命令是用Nx34大小重寫一個新文件,因爲我將最後一列轉換爲8個二進制位 – 2010-11-30 04:47:30

相關問題