How to Merge the cells and name?

2 views (last 30 days)
C PRASAD
C PRASAD on 6 Jul 2022
Commented: C PRASAD on 6 Jul 2022
I have the data and I would like to name and merge.The above table the data i have, the below table the table i required.
  1 Comment
Walter Roberson
Walter Roberson on 6 Jul 2022
How does this differ from the previous similar questions you have posted on the topic?

Sign in to comment.

Accepted Answer

Chunru
Chunru on 6 Jul 2022
datayouhave = rand(8, 5); % replace this with your data
t = array2table(datayouhave, "VariableNames", ["Maths", "Physics", "Politics", "Economy", "English"]);
name = ["AAA", "AAA", "AAA", "BBB", "BBB", "BBB", "CCC", "CCC"]';
t = [array2table(name) t];
t
t = 8×6 table
name Maths Physics Politics Economy English _____ _______ _______ ________ ________ _________ "AAA" 0.12481 0.4245 0.89831 0.22978 0.0087086 "AAA" 0.19179 0.89669 0.25311 0.16138 0.49893 "AAA" 0.60817 0.29709 0.65617 0.49247 0.79378 "BBB" 0.18862 0.49918 0.19054 0.047833 0.3502 "BBB" 0.48838 0.55171 0.51338 0.066283 0.39933 "BBB" 0.43513 0.48865 0.21088 0.21244 0.88148 "CCC" 0.25775 0.61049 0.75696 0.80927 0.74499 "CCC" 0.64411 0.27928 0.11384 0.13664 0.14539
  3 Comments

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 6 Jul 2022
You cannot create that kind of table in MATLAB.
In order to have a row name span multiple rows, you will need to create a table with three rows and two variables. The first variable will be the Name. For each row, the second variable must be a complete table.
M = round(randn(15, 5), 1)
M = 15×5
-0.2000 -0.5000 -0.1000 -0.9000 1.6000 2.0000 0.5000 0.6000 1.1000 -0.1000 0.4000 1.1000 -0.7000 -1.1000 1.0000 0.1000 0.5000 2.1000 0.8000 -0.4000 0.7000 1.1000 0.5000 -0.4000 1.1000 -0.4000 -0.4000 0.8000 1.5000 -0.7000 -0.7000 -0.2000 0.6000 0.4000 0.6000 0 0.7000 -0.3000 -0.4000 -0.6000 -0.9000 -1.3000 -2.0000 2.4000 -2.2000 -0.4000 -0.6000 -0.7000 1.1000 1.1000
varnames = ["Maths", "Physics", "Politics", "Economy", "English"];
T1 = array2table(M(1:5,:), 'VariableNames', varnames);
T2 = array2table(M(6:10,:), 'VariableNames', varnames);
T3 = array2table(M(11:15,:), 'VariableNames', varnames);
names = ["Ramu"; "Rihith"; "Swarmy"];
Output = table('Size', [3 2], 'VariableTypes', ["string", "table"], 'VariableNames', ["Name"; "Marks"]);
Output.Name = names;
Output.Marks = {T1; T2; T3};
Output
Output = 3×2 table
Name Marks ________ ___________ "Ramu" {5×5 table} "Rihith" {5×5 table} "Swarmy" {5×5 table}
Output.Marks{1}
ans = 5×5 table
Maths Physics Politics Economy English _____ _______ ________ _______ _______ -0.2 -0.5 -0.1 -0.9 1.6 2 0.5 0.6 1.1 -0.1 0.4 1.1 -0.7 -1.1 1 0.1 0.5 2.1 0.8 -0.4 0.7 1.1 0.5 -0.4 1.1
So you can construct the table, but it will not display nicely.

Community Treasure Hunt

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

Start Hunting!