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

조회 수: 8 (최근 30일)
Dave
Dave 2021년 10월 13일
댓글: Walter Roberson 2021년 10월 13일
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
Dave
Dave 2021년 10월 13일
Update, I set it to char outside the loop, it works.
Danke
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개)

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by