How to copy variable titles from one table and copy them in another table

43 views (last 30 days)
I have copied some data from a large table into a small table with the same variable. Now I want to copy the vriable names from the first table in to the second table.
How can I do that?
Example:
A B C D
0 0 1 2
5 7 5 5
8 6 7 4
2 1 1 1
new table should be like:
A B C D
0 7 5 5
8 1 1 1

Answers (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 5 Nov 2021
You should employ renamevars(), e.g.:
clearvars; clc
T = table([3; 4; 2; 1; -1;]);
T.A = {'C1'; 'D1'; 'E1'; 'F1'; 'H1'};
T.B = [3; 4; 2; 1; 0; ];
T.C = {'ABC'; 'CBA'; 'ACB'; 'CAB'; 'BAC'};
T3 = table([3;4; 2]);
T3.CL = {'C1'; 'D1'; 'E1'};
T3.HS = [3; 4; 2];
T3.GS = {'ABC'; 'CBA'; 'ACB'};
NEWname= T.Properties.VariableNames;
OLDname = T3.Properties.VariableNames;
T3 = renamevars(T3, OLDname, NEWname) % Assigned new variable names to [T3] taken from [T] table

Community Treasure Hunt

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

Start Hunting!