append rows of a table

So i would like to take two tables lets say T1 and T2
I want to take a single line if T2 and append it to the end of T1.
I found this could as an example. It works just fine except it appends the enitre table. I want to just append a single row from T2 and tack it on to the end of T1.
Anyone have any ideas?
T2 = readtable('morePatients.csv');
Tnew = [T;T2];
size(Tnew)

5 commentaires

Robert
Robert le 27 Juil 2021
Nevermind , i got it
Tnew = [T;T2{1,1:end}];
That probably is NOT what you want. How would you append one row of a double matrix to another double matrix?
x1 = [X1; x2(1,:)]
right? Same thing for tables. Very often with tables, ak yourself, "how would I do this with doubles?" What you've shown may work by happenstance for some obscure cases. but is definitely not the best way. In general, T2{1,:} is going to error because the vars in T2 are different types and can't be concatenated. Braces extract. T2{1,:} is no longer a table.
Maybe your T2 contains only cell arrays, in which case it is just by happenstance. A table with only cell arrays is a bit suspect, you may be doing something suboptimal. With no information to go on, hard to give any advice.
Walter Roberson
Walter Roberson le 27 Juil 2021
Considering recent discussion, Robert just might be using a table of cell array of character vectors.
Tnew = [T;T2(1,:)];
maybe? This would only work if the variable names match (though iirc if the names match but are in a different order then the entries will be matched by variable name.)
Peter Perkins
Peter Perkins le 28 Juil 2021
Correct. If the two sets of var names differ only by permutation, vertcat matches them up. If the two sets are more different than that, that's a whole 'nuther question.

Connectez-vous pour commenter.

Réponses (1)

Walter Roberson
Walter Roberson le 27 Juil 2021

0 votes

The general solution is to vertcat or [;] a cell array that has the same number of columns that as the table has variables. This will work even if the table contains multiple data types. Values being appended will have their type converted if necessary.
You might need to use table2cell if your table has multiple data types.
Or if the variable names match you can just vertcat T2(1,:)

Produits

Version

R2018b

Tags

Question posée :

le 27 Juil 2021

Commenté :

le 28 Juil 2021

Community Treasure Hunt

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

Start Hunting!

Translated by