Create a table with 2 header lines followed by numeric data
조회 수: 42 (최근 30일)
이전 댓글 표시
header line 1 = string
header line 2 = string
line 3:end = numeric data
How do I create a table with two header lines followed by the numeric data?
I have been using table(numeric data, 'VariableNames', header line 1) but cannot find a way to have two header lines before the numeric data begins.
Any help would be much appreciated.
댓글 수: 0
채택된 답변
Dheeraj
2024년 7월 22일
Hi Phil Roberts,
I understand you want to create a table with two header lines.
While the "table" function in MATLAB does not natively support multiple header lines, you can achieve a similar effect using a combination of "table" and "cell arrays". Below is a method to create a table-like structure with two header lines for display purposes,
% Sample data
header1 = {'HeaderLine1', 'HeaderLine2', 'HeaderLine3'};
header2 = {'SubHeaderLine1', 'SubHeaderLine2', 'SubHeaderLine3'};
numeric_data = rand(5, 3); % Example numeric data
% Create the numeric data table
T = array2table(numeric_data, 'VariableNames', header1);
% Create a cell array to hold the complete display with both headers
completeData = [header1; header2; num2cell(numeric_data)];
% Display the table with both headers
disp(completeData);
In this approach, the "completeData" cell array combines your headers and numeric data for display. While the "table" object "T" can be used for performing operations on the numeric data, the "completeData" cell array will be useful for displaying both header lines.
Thank you.
추가 답변 (1개)
Piyush Kumar
2024년 7월 22일
Hi Phil,
As mentioned in the documentation link, Tables store columns of data in variables. As you can see in the below mentioned example, the "patients" table stores data of many variables like "LastName", which is a header in the table too.
Having 2 headers is like storing 2 vairables in a single table column. Can you please explain your use-case?
If you just want to highlight the first row of data, you may try something like this -
LastName = ["<strong>Sanchez</strong>";"Johnson";"Zhang";"Diaz";"Brown"];
Age = ["<strong>38</strong>";43;38;40;49];
Smoker = ["<strong>true</strong>";false;true;false;true];
Height = ["<strong>71</strong>";69;64;67;64];
Weight = ["<strong>176</strong>";163;131;133;119];
BloodPressure = ["<strong>124</strong>" "<strong>93</strong>"; 109 77; 125 83; 117 75; 122 80];
patients = table(LastName,Age,Smoker,Height,Weight,BloodPressure)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!