Hex to Signed Int conversion of 16bit values in a Table with data type 'cell array of character vectors'

4 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

Walter Roberson
Walter Roberson el 15 de Ag. de 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 comentarios
manoj hanu
manoj hanu el 16 de Ag. de 2019
Sorry for streching this one long. That works But, completely becomes a new matrix. I would like to have the table intact with the column names.
Walter Roberson
Walter Roberson el 16 de Ag. de 2019
NewTable = [YourTable(:,1:2), array2table(output)];
NewTable.Properties.VariableNames(3:end) = Table.Properties.VariableNames(3:end);

Iniciar sesión para comentar.

Más respuestas (1)

Guillaume
Guillaume el 16 de Ag. de 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
  1 comentario
manoj hanu
manoj hanu el 16 de Ag. de 2019
The first one looks really helpful and would save a lot of time. But, unfortunately I am using 2018a. Thanks for the answer.

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos


Versión

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by