Add a variable to table

Hi,
I want to join 2 tables together.
In order to do that, table 1 must have the same number of columns as table 2.
table 1 has 8 variables
table 2 has 9 variables
So, I want to add a column to table 8
The variable will be of no use - it will just be a kind of dummy variable
How can I do this?
Thank you.

Respuestas (1)

Turlough Hughes
Turlough Hughes el 15 de Dic. de 2019
Editada: Turlough Hughes el 15 de Dic. de 2019

0 votos

Hey Sarah,
Let's say you have two tables, as follows:
T1=table((1:10).',(10:-1:1).');
T2=table((11:20).',(20:-1:11).',(1:10).');
To add a dummy column you could write the following;
T1.dummy=NaN(size(T1,1),1);
To concatenate them, ensure firstly that the VariableNames are the same, which ought to be the case anyway except for T1.dummy:
T1.Properties.VariableNames=T2.Properties.VariableNames;
You can then concatenate the tables as follows:
T=[T1;T2];
Finally, with this approach I assume that the values in the last column of T2 are still important (otherwise you could just write a table without the last column of T1)
T=[T1;T2(:,1:end-1)];
EDIT: following comments below.

4 comentarios

Sarah Yun
Sarah Yun el 15 de Dic. de 2019
Hi, thanks.
I want to concatenate them vertically.
Turlough Hughes
Turlough Hughes el 15 de Dic. de 2019
Ah ok. Do the variables have the same names?
Turlough Hughes
Turlough Hughes el 15 de Dic. de 2019
Editada: Turlough Hughes el 15 de Dic. de 2019
For example if you had:
T1=table((1:10).',(10:-1:1).');
T2=table((11:20).',(20:-1:11).',(1:10).');
You could concatenate them as follows:
T=[T1; T2(:,1:end-1)];
Turlough Hughes
Turlough Hughes el 15 de Dic. de 2019
Did this work for you?

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

el 15 de Dic. de 2019

Comentada:

el 15 de Dic. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by