Search code examples
matlablayoutmatlab-table

How to give a title to a matlab table?


I have a few tables, that after producing within the command window I need to print-screen into a word document. Is it possible to give the table a title so it does not need further labelling within the report.

Or is there a way to remove where it say the size of the table. Below is my code for generating the table. Are there any better ways to create the table in the first place. Most searches only return details on chaing column and/or row title.

T = table(Ureal(:,j), Umeth(:,j), Udiff(:,j), 'VariableNames',{'Exact', ... 'Numerical ','Difference'})

Thanks!


Solution

  • table do not have title. One workaround is to print a text first then print the table. Taking a example table from MATLAB:

    LastName = {'Sanchez';'Johnson';'Li';'Diaz';'Brown'};
    Age = [38;43;38;40;49];
    Smoker = logical([1;0;1;0;1]);
    Height = [71;69;64;67;64];
    Weight = [176;163;131;133;119];
    BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];
    T = table(LastName,Age,Smoker,Height,Weight,BloodPressure);
    
    title = 'Table 1. My patients';
    
    disp(sprintf('%40s',title)) % allocate 40 character spaces for the title.
    disp(T)
    

    Output:

                    Table 1. My patients
    LastName     Age    Smoker    Height    Weight    BloodPressure
    _________    ___    ______    ______    ______    _____________
    
    'Sanchez'    38     true        71       176       124     93  
    'Johnson'    43     false       69       163       109     77  
    'Li'         38     true        64       131       125     83  
    'Diaz'       40     false       67       133       117     75  
    'Brown'      49     true        64       119       122     80  
    

    Check sprintf also if you are not familiar with it.