Create a table with 2 header lines followed by 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 Comments

Accepted Answer

Dheeraj on 22 Jul 2024

Direct link to this answer

Cancel Copy to Clipboard

Direct link to this answer

Cancel Copy to Clipboard 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 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.

1 Comment
Phil Roberts on 22 Jul 2024

Direct link to this comment

Cancel Copy to Clipboard

Direct link to this comment

Cancel Copy to Clipboard Hello Dheeraj, This works great! Thank you for your help! Many thanks,

More Answers (1)

Piyush Kumar on 22 Jul 2024

Direct link to this answer

Cancel Copy to Clipboard

Direct link to this answer

Cancel Copy to Clipboard

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 = [ "Sanchez" ; "Johnson" ; "Zhang" ; "Diaz" ; "Brown" ]; Age = [ "38" ;43;38;40;49]; Smoker = [ "true" ;false;true;false;true]; Height = [ "71" ;69;64;67;64]; Weight = [ "176" ;163;131;133;119]; BloodPressure = [ "124" "93" ; 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"