Hex to Signed Int conversion of 16bit values in a Table with data type 'cell array of character vectors'
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
manoj hanu
le 15 Août 2019
Commenté : Walter Roberson
le 16 Août 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 commentaires
Walter Roberson
le 15 Août 2019
Please show
class(YourTable.AC2)
replacing YourTable with the name of your table.
Réponse acceptée
Walter Roberson
le 15 Août 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 commentaires
Walter Roberson
le 16 Août 2019
NewTable = [YourTable(:,1:2), array2table(output)];
NewTable.Properties.VariableNames(3:end) = Table.Properties.VariableNames(3:end);
Plus de réponses (1)
Guillaume
le 16 Août 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
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!