readTable() interprets columns as numeric if first 250 entries are empty

I am using readTable() to import a tab-delimited .txt file with many rows (~10,000) and several columns of non-numeric data. Some of the columns have many empty entries, which I have stored as empty quotes ("").
For most columns, readTable is able to import the data just fine. However, I noticed that some columns are interpreted as numeric, and therefore the entire column is imported as NaNs. I discovered that this happens for columns whose first ~250 rows are all empty, even the rest of their rows are not empty.
I know that I can explicitly specify that my format type is all strings, but it seems to me that the readTable function should be able to automatically detect this, especially when I'm using quotes for all entries in the .txt file (even the empty entries). Also, I need to use this function for .txt files that have both numeric and text columns, and I really don't want to hard-code the expected format type of every column...

1 Comment

Thanks, I had the same issue, when a column has sparse string, and matlab read its content as NaN.

Sign in to comment.

 Accepted Answer

I'm answering my own question, but figured it would be useful to others who would like a one-line solution.
Strangely, when I use detectImportOptions on my file, it properly interprets the text columns as text (even with many leading empty rows). Therefore, the following line worked for me, even though it looks weird:
x = readtable("data.txt", detectImportOptions("data.txt"));

1 Comment

Three years later, I owe you a big debt of gratitude, internet stranger.
It took me a long time to figure out why my file import was failing to import some (but not all) of my datetime fields, and in particular that it had to do with the "magic number" of 250 you discovered. Searching 250 readtable matlab found your post, and the trick in your answer worked for me.
Thanks!
P.S. By the power vested in me as an Editor, I've officially accepted your answer. :-)

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2020b

Community Treasure Hunt

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

Start Hunting!