Concatenating tables with different format

4 visualizaciones (últimos 30 días)
Wesso
Wesso el 28 de En. de 2019
Comentada: Walter Roberson el 28 de En. de 2019
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?

Respuesta aceptada

Walter Roberson
Walter Roberson el 28 de En. de 2019
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 comentarios
Wesso
Wesso el 28 de En. de 2019
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 el 28 de En. de 2019
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 .

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Tables en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by