I have two files with different rows but with the same number of columns and I want to combine it together but I am getting an error * All tables in the bracketed expression must have the same number of rows. *
file1 = readtable('306K-268K.csv'); file2 = readtable('266K-228K.csv'); Com = [file1 file2];
Thanks a lot for the help

 Respuesta aceptada

Star Strider
Star Strider el 2 de Nov. de 2018
You probably want the outerjoin (link) function:
Com = outerjoin(file1,file2);
See if that does what you want.

4 comentarios

Shelender Kumar
Shelender Kumar el 2 de Nov. de 2018
Thanks but it is adding extra NAN in each row
Star Strider
Star Strider el 2 de Nov. de 2018
The outerjoin function has additional arguments that you can use that could provide the result you want. It appears to be the only one of that group of functions that is compatible with your data.
MattyK
MattyK el 9 de Feb. de 2022
Thanks very much for this solution, have always wanted it. It works perfectly
Star Strider
Star Strider el 9 de Feb. de 2022
@MattyK My pleasure!

Iniciar sesión para comentar.

Más respuestas (4)

madhan ravi
madhan ravi el 2 de Nov. de 2018
Editada: madhan ravi el 17 de Nov. de 2018

2 votos

You can't merge table with different number of size dimensions , use structure instead

8 comentarios

thanks, let us say I have two sets A and B which looks like this
A =
1.38E-01 -1.02E-05 -8.81E-02
1.54E-01 NaN -5.51E-02
6.41E+01 7.83E-06 1.09E+02
B =
1.38E-01 -1.02E-05 -8.81E-02
1.54E-01 NaN -5.51E-02
answer=
here A and B are side by side
1.38E-01 -1.02E-05 -8.81E-02 1.38E-01 -1.02E-05 -8.81E-02
1.54E-01 NaN -5.51E-02 1.54E-01 NaN -5.51E-02
6.41E+01 7.83E-06 1.09E+02
madhan ravi
madhan ravi el 18 de Nov. de 2018
Editada: madhan ravi el 18 de Nov. de 2018
A = [1.38E-01 -1.02E-05 -8.81E-02
1.54E-01 NaN -5.51E-02
6.41E+01 7.83E-06 1.09E+02 ]
B = [1.38E-01 -1.02E-05 -8.81E-02
1.54E-01 NaN -5.51E-02 ;
zeros(1,size(A,2))] %PADDED WITH ZEROS because matrix dimensions should be the same when concatenated
side_by_side=[A B]
Thanks a lot, I made a mistake in asking question to you, actually A and B are csv file. for example if you follow thread on the top, you will find that I have attched two files
file1 = readtable('306K-268K.csv');
file2 = readtable('266K-228K.csv');
I wanted to combine file1 (dim is 642*40) and file2(681*40)
Thanks
madhan ravi
madhan ravi el 18 de Nov. de 2018
Editada: madhan ravi el 18 de Nov. de 2018
file1 = readtable('306K-268K.csv');
file2 = readtable('266K-228K.csv');
A=table2array(file1);
B=table2array(file2);
size(A)
size(B)
A=[A;zeros(size(B,1)-size(A,1),size(B,2))];
side_by_side = [A B]
size(side_by_side) %should be 681*80 if it is then we have achieved it
Shelender Kumar
Shelender Kumar el 18 de Nov. de 2018
Thanks, but I was thinking more of a genral solution, because I have 7 files like this, all of which have a differnt rows but the number of coloumns are same.
A=[A;zeros(abs(size(B,1)-size(A,1)),size(B,2))];
Shelender Kumar
Shelender Kumar el 18 de Nov. de 2018
Thanks a lot
madhan ravi
madhan ravi el 18 de Nov. de 2018
Glad to help :)

Iniciar sesión para comentar.

Stéphane Rainville
Stéphane Rainville el 16 de Nov. de 2018
You're missing a semi-colon to invoke vertical concatenation ('vertcat') rather than default horizontal concatenation ('horzcat').
For instance, two tables with different number of rows (but same number of columns), this does NOT work:
myBigTable = [myTable1 myTable2];
But this does:
myBigTable = [myTable1; myTable2];
I was facing a similar problem when storing tables of different lengths in a cell array.
myTables{1} = myTable1;
myTables{2} = myTable2;
and using
bigTable = [myTables{:}]
did not work because unwrapping and concatenating cell contents invoked horizontal concatenation. You can't just stick a semi-colon in there, so I had to explicitly invoke vertical concatenation like this:
bigTable = vertcat(myTables{:});

2 comentarios

Shelender Kumar
Shelender Kumar el 17 de Nov. de 2018
Editada: Shelender Kumar el 17 de Nov. de 2018
Thanks for the help
but I want to place my file1 and file2 side by side. That means it should give me the data like horizcat
Stéphane Rainville
Stéphane Rainville el 17 de Nov. de 2018
Ah I see. Then yeah, tables of different length would be a problem.

Iniciar sesión para comentar.

the cyclist
the cyclist el 2 de Nov. de 2018

0 votos

I'm guessing you need the join command.

1 comentario

Shelender Kumar
Shelender Kumar el 2 de Nov. de 2018
I have some NAN values in my column so I am getting this error The key variables for A and B cannot contain any missing values.

Iniciar sesión para comentar.

Peter Perkins
Peter Perkins el 6 de Nov. de 2018
"Merge" is kind of vague. It may be that you just need to add a semicolon to vertically concatenate:
Com = [file1; file2]

3 comentarios

Shelender Kumar
Shelender Kumar el 17 de Nov. de 2018
Thanks a lot but I want to place to data side by side not above and below
HabenG
HabenG el 3 de Nov. de 2021
Editada: HabenG el 3 de Nov. de 2021
Have you figured out the issues?? seems like there is no way to simply combine columns of different size matrrix, table, array etc...i feel like there should be a simple fuction for this
Peter Perkins
Peter Perkins el 8 de Nov. de 2021
The current way to do this is to create the same number of rows in the smaller matrix/table/whatever. That's already been shown in an earlier reply.

Iniciar sesión para comentar.

Categorías

Preguntada:

el 2 de Nov. de 2018

Comentada:

el 9 de Feb. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by