How to merge cells within a table

조회 수: 20 (최근 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 CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by