extracting data from hex

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!

Answers (2)

Guillaume
Guillaume on 29 May 2019
Edited: Guillaume on 29 May 2019
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 on 28 May 2019
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 Comments

Dear Rik,
Thanks for the answer. I get the following error when trying to use cellfun,
Cell contents reference from a non-cell array object.
However, aa is a cell as far I understood.. Here is the code,
a=readtable('2017-02-10-16-23-46.dat')
aa=table2cell(a)
data_dec=cellfun(@(x) hex2dec(x{1}(3:end)),aa(:,3));
I also tried with aa{:,3} instead and didn't work either. I would really parecciate your help.
Rik
Rik on 29 May 2019
I hardly ever work with the table datatype, so can you attach the data file (you'll probably have to zip it). Then I can run your code and make sure the syntax of my code matches your data.
Here is the data sample, thank you so much!
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
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))

Sign in to comment.

Categories

Asked:

on 28 May 2019

Commented:

on 29 May 2019

Community Treasure Hunt

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

Start Hunting!