Sub heading in matlab table

조회 수: 34 (최근 30일)
Davindra Usov
Davindra Usov 2023년 1월 16일
댓글: Davindra Usov 2023년 1월 17일
Hello,
I am trying to create the table shown in the image attached. So far my code generates the table with all the headings but without the 'old fruit' and 'new fruit' headings. How do I add these headings into my code?
table.Properties.VariableNames(1:4) = {'apples','oranges','strawberries','grapefruit'}
writetable(table,'fruit.csv')
thank you

채택된 답변

Adam Danz
Adam Danz 2023년 1월 16일
편집: Adam Danz 2023년 1월 16일
There isn't an option to arrange subheadings as described in your attached image.
However, there are several ways to rearrange the table to contain subheadings.
Option 1: nested subheaders
MATLAB R2018b or later
apples = table([3;4],[1;4],'VariableNames',{'old','new'});
oranges = table([3;4],[1;4],'VariableNames',{'old','new'});
strawberries = table([3;4],[1;4],'VariableNames',{'old','new'});
grapefruit = table([3;4],[1;4],'VariableNames',{'old','new'});
fruit = table(apples, oranges, strawberries, grapefruit)
fruit = 2×4 table
apples oranges strawberries grapefruit old new old new old new old new __________ __________ ____________ __________ 3 1 3 1 3 1 3 1 4 4 4 4 4 4 4 4
Option 2: nested subheaders version 2
MATLAB R2018b or later
old = table([3;4],[6;4],[5;3],[1;2], 'VariableNames', {'apples','ornages','strawberries', 'grapefruit'});
new = table([1;4],[1;13],[2;11],[3;3], 'VariableNames', {'apples','ornages','strawberries', 'grapefruit'});
fruit = table(old,new)
fruit = 2×2 table
old new apples ornages strawberries grapefruit apples ornages strawberries grapefruit _______________________________________________ _______________________________________________ 3 6 5 1 1 1 2 3 4 4 3 2 4 13 11 3
Option 3: Store subheaders in a table variable
MATLAB R2013b or later
Note that the use of subheadings above makes indexing difficult. Here's a recommendation that preserves the labels but allows for easier indexing:
fruit = table(["old";"old";"new";"new"], ...
[3;4;1;4],[6;4;1;13],[5;3;2;11],[1;2;3;3], ...
'VariableNames', {'age','apples','oranges','strawberries','grapefruit'})
fruit = 4×5 table
age apples oranges strawberries grapefruit _____ ______ _______ ____________ __________ "old" 3 6 5 1 "old" 4 4 3 2 "new" 1 1 2 3 "new" 4 13 11 3
  댓글 수: 1
Davindra Usov
Davindra Usov 2023년 1월 17일
Thank you so much!! I tried option 2 and it worked!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by