How to merge cells within a table

87 ビュー (過去 30 日間)
David Stolnis
David Stolnis 2017 年 6 月 8 日
コメント済み: Peter Perkins 2017 年 6 月 20 日
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.

採用された回答

Guillaume
Guillaume 2017 年 6 月 8 日
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)
  1 件のコメント
David Stolnis
David Stolnis 2017 年 6 月 8 日
Thank you Guillaume. I do have it duplicated currently. My goal was to reduce formatting after exporting to excel. Wishful thinking...

サインインしてコメントする。

その他の回答 (1 件)

Peter Perkins
Peter Perkins 2017 年 6 月 9 日
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 件のコメント
Guillaume
Guillaume 2017 年 6 月 19 日
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.
Peter Perkins
Peter Perkins 2017 年 6 月 20 日
OK, I get it. That's not possible.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by