How to merge cells within a table
19 views (last 30 days)
Show older comments
I have a data table in which I am expanding with calcualtions. I want to have a column that merges the sequential even with its subsequent odd.

I want column 6 to have the bordered cells merged with a result of a calculation inside. Is this possible? I've seen 2 column headers merged into one with 2 separate columns underneath (see Temperature in image below). I would assume something similar could be done. Thanks in advance for any help.

0 Comments
Accepted Answer
Guillaume
on 8 Jun 2017
No it's not possible to merge cells by row or even by columns. What you're seeing in the temperature column is not the merging of two different columns but the display of a 2 column matrix stored in the single temperature variable.
Note that you can easily perform your calculation on pair of rows with rowfun. However if you want the result back into your original table you'll have it to duplicate it for each pair, e.g.:
%demo table
t = table(datetime(2017, 1, 5, 'Format', 'dd/MM/yyyy HH:mm:ss') + hours(0:19)', ...
rand(20, 1)*7000, [randi([50 70], 20, 1), randi([25 40], 20, 1)], ...
'VariableNames', {'Date', 'Load', 'Temperature'})
%calculate mean load per pair of rows
pairedload = rowfun(@mean, [t, table(repelem(1:height(t)/2, 2)', 'VariableNames', {'Group'})], ...
'InputVariables', 'Load', 'GroupingVariables', 'Group')
%to reinsert into original table
t.MeanPairedLoad = repelem(pairedload.Var3, 2)
More Answers (1)
Peter Perkins
on 9 Jun 2017
David, this is certainly possible, but you have not said what you want done with the other variables in the table. Guillaume has provided a solution that merged each odd/even pair into a variable that's half the height of the original, and then broadcasts that out into the original as duplicate pairs of values. It sounds like that's not what you want. What do you want as the result?
7 Comments
Guillaume
on 19 Jun 2017
My understanding is that David wants some cells of the table to span more than one row or column. Similar to the rowspan or colspan of an html table or the merge cell feature of Excel.
That's the way I interpret the 1st illustration in the question. Column 60 would have half the number of elements as the other columns, with 1st element shared between row 1 and 2 of the other columns, 2nd element shared between row 3 and 4, etc.
See Also
Categories
Find more on Data Type Identification in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!