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

5 次查看(过去 30 天)
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...

采纳的回答

Jonathan Robinson
Jonathan Robinson 2020-10-11
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 个评论
the cyclist
the cyclist 2023-3-28
编辑:the cyclist 2023-3-28
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. :-)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by