2014-01-17 76 views
2

INT16數據I來自加速計的數據記錄讀取該組的十六進制值的:讀取在Matlab

35AC,2889,1899,0C4A,058B,FD46,F620,F001,EE44,EF08,EF46,F750, 007F,0814,1369,21F3,34F0,45CE,5992,6D05,7C12,7FEF,7FF8,7FF8,7FF8,7FF8,7FD9,7F27,74A7,67D8,5826,468F,3621,2573,1326,0441,F88F, F1BF,F082,EADB,EAEE,EE04,F190,F89E,01F5,0B0C,155A,2721,3A20,48DC,5985,676A,721E,7C20,7FF8,7FEE,7F1B,

它應該以某種方式繪製一個正弦曲線,但我無法找到有符號的int16的正確導入方法,曲線從0跳到65535.

你能幫我嗎?

我已經試過sscanf(...,'%4x')

回答

0

sscanf格式簽署的十六進制INT16只是'%4i'。不幸的是,它預計十六進制值將從0x開始,而您顯然不會。一種可能性是您按原樣掃描它,然後手動將其轉換爲帶符號的int16。這樣做的一種可能的方法是以下內容:被讀取

input = sscanf(...,'%4x'); 
input16 = typecast(uint16(input),'int16'); 

的值作爲一個UINT32並自動轉換由sscanf功能加倍。因此,我們將它轉​​換爲uint16,然後將其轉換爲16位int(請注意,僅使用int16(input)不起作用,因爲它不會將INT16_MAX上的值轉換爲負值)。

0
string = '35AC,2889,1899,0C4A,058B,FD46,F620,F001,EE44,EF08,EF46,F750,007F,0814,1369,21F3,34F0,45CE,5992,6D05,7C12,7FEF,7FF8,7FF8,7FF8,7FF8,7FD9,7F27,74A7,67D8,5826,468F,3621,2573,1326,0441,F88F,F1BF,F082,EADB,EAEE,EE04,F190,F89E,01F5,0B0C,155A,2721,3A20,48DC,5985,676A,721E,7C20,7FF8,7FEE,7F1B'; 
%// Your data as a string 

string = [string ',']; %// add ending comma to reshape into groups of five chars 
strings = reshape(string,5,[]).'; 
strings = strings(:,1:4); %'// each row is 4 chars representing a hex number 
numbers = hex2dec(strings); %// convert each row to a number 
ind = numbers>=32768; 
numbers(ind) = numbers(ind)-65535; %// get rid of jumps 
plot(numbers) 

enter image description here

0

感謝很多

兩個以前的答案是there了。

sscanf替代方案假定速度更快。