How to include a wide table's VariableDescriptions as an additiolnal column when stacking the table?
Show older comments
I would like to make a wide table's VariableDescriptions as an additional column when I stack the table. I have written some codes to do it. They work (in a dirty manner). Is there a better way?
T = cell2table({'A', 2, 3, 3; 'B', 5, 5, 7}, "VariableNames", {'AB', 'x', 'y', 'z'});
% variable descriptions
T.Properties.VariableDescriptions = {'AB', 'xdes', 'ydes', 'zdes'};
S = stack(T, 2:4, "IndexVariableName",'xyz', 'NewDataVariableName','value')
% duplicate the table, using the variable descriptions as new variable names.
T2 = T;
T2.Properties.VariableNames = T.Properties.VariableDescriptions;
% stack the second table
S2 = stack(T2, 2:4, "IndexVariableName",'xyzdes', 'NewDataVariableName','value');
% add the descriptions as a new column to the first table
S.xyzdes = S2.xyzdes;
S = movevars(S, "xyzdes", "After", "xyz")
Accepted Answer
More Answers (0)
Categories
Find more on Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!