MATLAB not indexing table with correct data type, how to specify data type when indexing table?
11 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I wrote a script that takes in an excel table using the "readtable" command.
inputTable = readtable(completeTableFilePath,'Sheet',sheetChoiceFileName,'TextType','string');
This, to my knowledge, should import all the cells of the excel file as strings. One part of the excel file is a column that has hex numbers (they cold be just "92" or "13C" etc...).
I had a really long excel table (around 300 lines) that had this column of hex numbers. I tried the program with a smaller table, maybe only 8 lines, and now it is having issues, particularly in the hex column.
The code:
% I just added these for testing (the idName)
idName = upper(inputTable{currentRowNumber,'ID'});
idName
if upper(inputTable{currentRowNumber,'ID'}) ~= "TBD"
if upper(inputTable{currentRowNumber,'ID'}) ~= recurringID
recurringID = upper(inputTable{currentRowNumber,'ID'});
messageIDDecimal = hex2dec(recurringID);
end
end
As I said, I changed nothing about this between runs, it works perfectly with the large table and it does not work with the smaller table. When I try to run it with the smaller table (which is just the larger table with a lot of the rows chopped off) I get idName as a "double" data type.
I figured I could fix this, by instead of relying on MATLAB to have the correct data type (which it should anyways because I specified so earlier!) I force the data type of strings using string(...).
% idName for testing
idName = upper(string(inputTable{currentRowNumber,'ID'}));
idName
if upper(string(inputTable{currentRowNumber,'ID'})) ~= "TBD"
if upper(string(inputTable{currentRowNumber,'ID'})) ~= recurringID
recurringID = upper(string(inputTable{currentRowNumber,'ID'}));
messageIDDecimal = hex2dec(recurringID);
end
end
I ran the above code on the smaller table and got the error: <missing> string element not supported; error on line with the hex2dec. idName shows up as <missing>.
I changed the code again to show the raw table indexing:
% idName for testing
idName = upper(string(inputTable{currentRowNumber,'ID'}));
idName
idTest = inputTable{currentRowNumber,'ID'};
idTest
if upper(string(inputTable{currentRowNumber,'ID'})) ~= "TBD"
if upper(string(inputTable{currentRowNumber,'ID'})) ~= recurringID
recurringID = upper(string(inputTable{currentRowNumber,'ID'}));
messageIDDecimal = hex2dec(recurringID);
end
end
idTest was coming up as the double data type, until the final go. idName was <missing> and idTest was "NaN". To my knowledge, "NaN" is "Not a Number". It is giving an error because it's trying to input a hex number as a regular double data type. I cannot find a way to fix this. I already specified that the table is to be imported as strings. I cannot cast to this, because its not just holding the data, but being the wrong type. It is throwing an error and not holding the data at all.
There is nothing I can do, unless there is some way to make MATLAB only import as a specific data type. I am having a lot of issues, because MATLAB assumes data types. Maybe coming from C just has me think differently. I can see how it can be useful, but the fact that there is no way around it (that I know of), makes it not useful.
I ran the script with "idName" and "idTest" on the larget excel table. It properly imported them as strings. I was able to index and both idTest and idName showed as strings, even the hex numbers that only had the regular 0-9 numbers. So it is not the code. It is just MATLAB sometimes deciding to import as strings and sometimes to not.
Risposta accettata
Voss
il 19 Giu 2024
Setting 'TextType' is for specifying whether the data readtable imports as text should be returned as string arrays or character vectors. It does not set the imported data type in general, as you have found. For instance, if readtable determines that some data is numeric, then 'TextType' has no effect on that data since it is not text.
Example:
% the file's contents
type('test.csv')
% the problem
T = readtable('test.csv')
% a solution
opts = detectImportOptions('test.csv'); % default import options for the file
opts = setvartype(opts,'ID','string'); % set 'ID' variable to be of type 'string'
T = readtable('test.csv',opts) % readtable with the new options
11 Commenti
dpb
il 20 Giu 2024
",,,I cannot do those in the same line, but I can do one line per data type"
Yes, per setvartype documentation, the type argument must be a scalar. A seemingly useful enhancement would be to let both selection and type be cellstr or string arrays with a 1:1 correspondence.
There's also a feature vartype with a table that allows you to index by the current type and convertvars will let you then change by that indexing method. Keep poking at the doc and don't stop reading at the first line; study it in depth; there's a lot hidden in the bowels a cursory glance won't tell you.
Più risposte (1)
dpb
il 20 Giu 2024
Modificato: dpb
il 21 Giu 2024
There are even more alternatives available with setvaropts including identifying the column(s) as hex. This adds the limitation that have to use an integer type, but that can then be cast to a double.
c=[[{'ID1'};{'0x92'};{'0x2A'}], [{'ID2'};{'92'};{'2A'}]];
fn='test.csv';
writecell(c,fn)
type test.csv
opt=detectImportOptions(fn);
opt=setvaropts(opt,'ID2','NumberSystem','hex','Type','int32');
tT=readtable(fn,opt,'ReadVariableNames',1)
0 Commenti
Vedere anche
Categorie
Scopri di più su Text Files in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!