MATLAB not indexing table with correct data type, how to specify data type when indexing table?
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.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. 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. table, importing excel data, data import, data types MATLAB Answers — New Questions