stacking or appending tables with NaN and 'ABC' -- cell vs non-cell

8 次查看(过去 30 天)
Hello, with a loop and using readtable I am stacking a table on top of an existing table where var2 is sometimes NaN or sometimes 'ABC'
The var type of var2 in both cases reads char
When I try to stack one table over the other it prompts:
Cannot concatenate the table variable 'var2' because it is a cell in one table and a non-cell in another.
Is there a way to fix this?

采纳的回答

Walter Roberson
Walter Roberson 2021-10-13
If I understand correctly, you are looping reading multiple files, and want to put the results all into one large table. And that you have a column which will at least sometimes contain text.
The times that it complains about nan: is it possible those are times that the entire column for that variable is empty? The automatic detection will call empty columns as double, not char.
If this is the problem, then there are two potential ways to proceed:
  1. detectImportOptions each time and setvartype() to force it to text; or
  2. detectImportOptions only once at the beginning (forcing to text would not hurt), and then use those import options for all remaining files
  3 个评论
Walter Roberson
Walter Roberson 2021-10-13
opts = detectImportOptions(csvfilename{1});
opts = setvartype(opts, 'var2', 'char');
Then inside the loop,
out1 = readtable(csvfilename{ii}, opts);
This should be faster than what you already had: when you were using readtable() without passing in options, MATLAB would automatically invoke detectImportOptions on your behalf (for the last several years, not before that.)

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by