Concatenating tables with different format

5 次查看(过去 30 天)
Hi,
I am concatenating tables in a large loop. all these tables are imported from excel . sometimes Matlab imports the same column as in cells and sometimes as double which is leading to the following errors. For example:
Cannot concatenate the table variable 'TargetBook' because it is a cell in one table and a non-cell in another. the examples are numerous for different columns.
How can I force the concatenation by transforming one column type to fit the precedent table column type in order to concatenate successfully?
in other words
for k = 1:numel(List)
File = fullfile(Base,List(k).name);
Result = readtable(File);
if k==1
t20172018=Result;
else
t20172018=[t20172018;Result];
end
end
%% How can I make sure that Result concatenate to t20172018 no matter what the format of the column in Result is?

采纳的回答

Walter Roberson
Walter Roberson 2019-1-28
detectImportOptions on one of the files that gives you numeric results . Then readtable passing in the detected options each time.
It sounds as if some of the files have text notes in columns intended to be numeric . If so then processing those might be tricky .
  2 个评论
Wesso
Wesso 2019-1-28
I am using the detectImportOptions but without luck
for example, I am getting Cannot concatenate the table variable 'TargetPrimarySICCode' because it is a cell in one table and a non-cell in another.How can I overcome this problem ?
List = dir(fullfile(Base,'*.xlsx'));
Result = cell(1,numel(List));
for k = 1:numel(List)
File = fullfile(Base,List(k).name);
if k==1
Result = readtable(File); %
t20172018=Result;
opts = detectImportOptions(File);
else
Result = readtable(File,opts,'ReadVariableNames',false);
t20172018=[t20172018;Result];
end
end
Walter Roberson
Walter Roberson 2019-1-28
detect options first and use them for the first readtable . option detection is better at figuring out needed datatypes so readtable without it can give different types than with it, especially for r2018b with fields that look like times which option detection converts to duration but without detection is a cell array of character .

请先登录,再进行评论。

更多回答(0 个)

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by