Merge tables with different dimensions?

Hi,
I'm trying to use vertcat to add together a sequence of tables. The problem is, some are 24x22, some are 24x19, etc.
Matlab returns the error
Error using table/vertcat (line 56)
All tables in the bracketed expression must have the same number
of variables.
Is there a way to work around this? Like, adding empty columns?
Thanks so much.

 Réponse acceptée

Guillaume
Guillaume le 19 Fév 2015
tables like matrices must have the same number of columns (variables with regards to tables) to concatenate them vertically. In addition, for tables, the variable names must be identical.
You can of course add variables to either table until all the variable names match:
t1 = array2table(magic(5), 'VariableNames', {'a', 'k', 'o', 'p', 'w'});
t2 = array2table(magic(6), 'VariableNames', {'a', 'p', 'o', 'v', 's', 'k'});
t1colmissing = setdiff(t2.Properties.VariableNames, t1.Properties.VariableNames);
t2colmissing = setdiff(t1.Properties.VariableNames, t2.Properties.VariableNames);
t1 = [t1 array2table(nan(height(t1), numel(t1colmissing)), 'VariableNames', t1colmissing)];
t2 = [t2 array2table(nan(height(t2), numel(t2colmissing)), 'VariableNames', t2colmissing)];
t = [t1; t2]

6 commentaires

Chelsea
Chelsea le 19 Fév 2015
Thanks!!!
I have a quick question, if you don't mind....Some of the columns have string inputs. When some of the data I'm trying to merge is missing values - and I insert NaN, this code won't allow me to merge NaNs with strings.
Error using table/vertcat (line 144)
Cannot concatenate the table variable 'Stimuli' because it is a cell in one table and a non-cell in another.
Is there an easy solution to this?
Thanks again!
Sure, replace the NaNs by empty cells:
t1 = array2table(magic(5), 'VariableNames', {'a', 'k', 'o', 'p', 'w'})
t2 = array2table(magic(6), 'VariableNames', {'a', 'p', 'o', 'v', 's', 'k'});
t2.v = repmat({'string'}, height(t2), 1)
t1colmissing = setdiff(t2.Properties.VariableNames, t1.Properties.VariableNames);
t2colmissing = setdiff(t1.Properties.VariableNames, t2.Properties.VariableNames);
t1 = [t1 array2table(nan(height(t1), numel(t1colmissing)), 'VariableNames', t1colmissing)];
t2 = [t2 array2table(nan(height(t2), numel(t2colmissing)), 'VariableNames', t2colmissing)];
for colname = t1colmissing
if iscell(t2.(colname{1}))
t1.(colname{1}) = cell(height(t1), 1);
end
end
for colname = t2colmissing
if iscell(t1.(colname{1}))
t2.(colname{1}) = cell(height(t2), 1);
end
end
t = [t1; t2]
Ross Nichols
Ross Nichols le 20 Août 2019
Thank you so much! This code saved my baloney for my project!
Ayana Cameron
Ayana Cameron le 10 Avr 2020
How would this change to horizontally concatenate?
Marguerite Kennish
Marguerite Kennish le 21 Août 2020
I would like to repeat Ayana Cameron's question:
"How would this change to horizontally concatenate?"
I am trying to build a table by adding a column each iteration of a loop, but the rows of each column has different rows.

Connectez-vous pour commenter.

Plus de réponses (2)

Sterling Baird
Sterling Baird le 5 Sep 2020
Modifié(e) : Sterling Baird le 5 Sep 2020

1 vote

I built on Guillaume's answer a while back, and recently ended up making and submitting a FEX function (tblvertcat) that supports cells in addition to other types which are supported by "missing" (double, char, etc.).
EDIT: I revamped the code to use outerjoin()
Evan
Evan le 19 Fév 2015
Modifié(e) : Evan le 19 Fév 2015
Does this example do what you need?
A = randi(9,4,3)
B = randi(9,4,5)
C = randi(9,4,2)
D(1:4,1:size(A,2)) = A;
D(5:8,1:size(B,2)) = B;
D(9:12,1:size(C,2)) = C
You could then assign individual columns to new arrays which could be used as arguments to the table function. This would give you your zero padding for mismatched rows.
Note that if you are using the table function as sending in your arrays in the fashion
table(A,B,C)
you shouldn't receive errors so long as your variables all have an equal number of rows. It sounds like this isn't what you're doing, but without knowing exactly the way you want to arrange your data via some sample code, it's hard to say more.

Catégories

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by