Create a table with 2 header lines followed by numeric data

51 次查看(过去 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.

采纳的回答

Dheeraj
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
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)
patients = 5x6 table
LastName Age Smoker Height Weight BloodPressure _________ ____ _______ ______ ______ _____________ "Sanchez" "38" "true" "71" "176" "124" "93" "Johnson" "43" "false" "69" "163" "109" "77" "Zhang" "38" "true" "64" "131" "125" "83" "Diaz" "40" "false" "67" "133" "117" "75" "Brown" "49" "true" "64" "119" "122" "80"

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by