Reading a hex table with some undefined values

9 ビュー (過去 30 日間)
Robert
Robert 2020 年 8 月 9 日
コメント済み: Walter Roberson 2020 年 8 月 10 日
I have a text table with hex values. Some of the values are of the form 0xXXXX. I'd like to read in the entire table as doubles with the 0xXXXX entries read as NaN. How do I do that?
  3 件のコメント
Robert
Robert 2020 年 8 月 10 日
All values are in the form of 5 digit hex values such as 0x1BEEF or 0x4DEAD. Some values are 0xXXXXX indicating an undefined value. The columns which have any 0xXXXXX entries are interpreted as strings. Any column with all legal hex values are interpreted as uint32. Ideally it would be nice if this could work with any size hex value. Here's a partial line:
0x00000 0xXXXXX 0xXXXXX 0xXXXXX 0xXXXXX 0xXXXXX 0x0002D 0x0002D
Robert
Robert 2020 年 8 月 10 日
I should mention I used readtable in 2020a.

サインインしてコメントする。

採用された回答

Robert
Robert 2020 年 8 月 10 日
Thanks for even looking at this. That example helped. But to complete the answer to my original question (convert everything to double and use NaN to mark 0xXXXXX entries) I did this:
% read in data
tbl = readtable('hex.txt');
data = table2cell(tbl);
% find XXXXX entries
missingindication = '0xXXXXX';
idx = find(strcmp(missingindication, data));
% read again
tbl = readtable('hex.txt', 'treatasmissing', missingindication, 'multi', true);
data = double(table2array(tbl));
data(idx) = NaN;
I don't know if that's the most efficient but it works. Anyway, thanks for your help.
  1 件のコメント
Walter Roberson
Walter Roberson 2020 年 8 月 10 日
If it was the "convert to double" that is the reason not to use the code I posted, then after my code use
varnames = data.Properties.VariableNames;
data = varfun(@double, data, ''); %changes variable names
data.Properties.VariableName = varnames;

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2020 年 8 月 10 日
filename = 'hex.txt';
missingindication = {'0xXXXXX'};
data = readtable(filename, 'treatasmissing', missingindication, 'readvariable',false, 'delimiter', ' ', 'multi', true);
This code will work for up to 16 hex digits; any column that has a value 2^32 or larger will be returned as uint64 .
If the missing data indicator itself can have different numbers of X characters, then you can expand the cell array, such as
missingindication = arrayfun(@(c) ['0x', repmat('X', 1, c)], 1:32, 'uniform', 0);

カテゴリ

Help Center および File ExchangeContour Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by