Hex to Signed Int conversion of 16bit values in a Table with data type 'cell array of character vectors'
7 views (last 30 days)
Show older comments
manoj hanu
on 15 Aug 2019
Commented: Walter Roberson
on 16 Aug 2019
I have a table as below having huge number of rows.
Time Identifier AC1 AC2 AC3 AC4
00:06:40.23 "300" "6500" "6900" "6D00" "7500"
00:06:40.25 "100" "B7FF" "E5FF" "7D10" "0100"
... ... ... ... ... ...
The columns AC1 to AC4 are in 16bit 2's complement representation.
Need to convert all the values of those 4 columns in to signed decimal vlaue. The table shld be intact.
Any help is appreciated. Also, would like to know what would be a convenient way to have these kind of data other than in table, so that would be helpful for further processing. The data being sensor values wrt time.
2 Comments
Walter Roberson
on 15 Aug 2019
Please show
class(YourTable.AC2)
replacing YourTable with the name of your table.
Accepted Answer
Walter Roberson
on 15 Aug 2019
temp = uint16(arrayfun(@(S) sscanf(S, '%x'), YourTable{:,3:6}));
output = reshape( typecast(temp(:), 'int16'), size(temp));
What kind of processing do you want to do on the entries? Possibly using a timetable() would be appropriate.
4 Comments
Walter Roberson
on 16 Aug 2019
NewTable = [YourTable(:,1:2), array2table(output)];
NewTable.Properties.VariableNames(3:end) = Table.Properties.VariableNames(3:end);
More Answers (1)
Guillaume
on 16 Aug 2019
It returned 'cell'
Assuming that the "" in your example are actual double quotes and not single quotes, then you've not created your tables very well. The type of these columns should be string. A cell array of scalar string is a big waste of memory.
Anyway, it doesn't matter since we're converting it to numeric.
Assuming, R2018b or later:
yourtable = convertvars(yourtable, {'AC1', 'AC2', 'AC3', 'AC4'}, @(var) cellfun(@(s) sscanf(s, '%x'), var))
on earlier versions, it's a bit more complicated as you can't easily change the type of column. This should work, assuming the AC columns are the last 4:
orignames = yourtable.Properties.VariableNames(end-3:end);
yourtable = [yourtable(:, 1:end-4), ...
varfun(@(var) cellfun(@(s) sscanf(s, '%x'), var), yourtable(:, end-3:end))];
yourtable.Properties.VariableNames(end-3:end) = orignames; %varfun prepends Fun_ to the variable it processes
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!