MATLAB Answers

Concatenating tables with different format

34 views (last 30 days)
Wesso
Wesso on 28 Jan 2019
Commented: Walter Roberson on 28 Jan 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?

Accepted Answer

Walter Roberson
Walter Roberson on 28 Jan 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 Comments
Walter Roberson
Walter Roberson on 28 Jan 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 .

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by