extracting data from hex

조회 수: 11 (최근 30일)
Luisa Cencha
Luisa Cencha 2019년 5월 28일
댓글: Guillaume 2019년 5월 29일
Dear all,
I have a .dat file whit hex data. I used 'readtable' to extract the info, but now I want to convert it to decimal using hex2dec. However I had the following error,
Error using hex2num>hex2decImpl (line 57)
Input to hex2num should have just 0-9, a-f, or A-F.
Error in hex2num (line 25)
x = hex2decImpl(char(s));
The lines I used,
a=readtable('2017-02-10-16-23-46.dat')
a.(3)=hex2num(a.(3))
..and a part of the data I'm trying to convert,
{'0x0000D830'}
{'0x000103EA'}
{'0x000105C1'}
{'0x000079B5'}
{'0x0000E24D'}
{'0x0000BB9F'}
{'0x0000A28F'}
There is any way of remove the 0x000 part? Or any other idea to convert and plot the hex data?
Thanks a lot!

답변 (2개)

Guillaume
Guillaume 2019년 5월 29일
편집: Guillaume 2019년 5월 29일
Easiest way is to use sscanf instead of hex2dec. sscanf with the '%x' format specifier understand '0x' notation directly:
>> sscanf('0x000103EA', '%x')
ans =
66538
With your table the simplest way to convert everything would be:
t = readtable('2017-02-10-16-23-46.dat'); %read as table
t = fillmissing(t, 'constant', '0x0', 'DataVariables', @iscellstr); %replace empty entries by 0x0 for cellstr variables
t = convertvars(t, @iscellstr, @(var) cellfun(@(x) sscanf(x, '%x'), var)) %convert cellstr variables to numbers with sscanf
Note that you may want to store the numbers as uint32 instead of double, in which case that last line becomes:
t = convertvars(t, @iscellstr, @(var) cellfun(@(x) uint32(sscanf(x, '%x'), var)))
One of the major advantage of sscanf over hex2dec is that it can decode 64-bit hexadecimal integers (with the '%lx' format). hex2dec will return rounded results for integers above flintmax:
>> sscanf('0xFFFFFFFFFFFFFFFE', '%lx') %returns the correct result
ans =
uint64
18446744073709551614
>> uint64(hex2dec('FFFFFFFFFFFFFFFF')) %returns an incorrect result due to the conversion to double
ans =
uint64
18446744073709551615

Rik
Rik 2019년 5월 28일
You should only remove the leading 0x as that indicates the data is hexadecimal.
data_hex={{'0x0000D830'}
{'0x000103EA'}
{'0x000105C1'}
{'0x000079B5'}
{'0x0000E24D'}
{'0x0000BB9F'}
{'0x0000A28F'}};
data_dec=cellfun(@(x) hex2dec(x{1}(3:end)),data_hex);
  댓글 수: 5
Rik
Rik 2019년 5월 29일
You can use Guillaume's suggestion, but the code below works (and fills empty positions with NaN).
a=readtable('2017-02-10-16-23-46.dat');
aa=table2cell(a);
data_dec=cellfun(@parse_fun,aa(:,3:6));
%if you are sure there are no empty cells you don't need parse_fun
%data_dec=cellfun(@(x) hex2dec(x(3:end)),aa(:,3));
function val=parse_fun(str)
if ~isempty(str)
val=hex2dec(str(3:end));
else
val=NaN;
end
end
Guillaume
Guillaume 2019년 5월 29일
Rather than converting to cell, you can work directly on the table with rowfun, varfun or convertvar:
a = convertvar(a, 3:6, @(var) cellfun(@parsefun, var))

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by