To create a DOM API table from a MATLAB® table, use an mlreportgen.dom.MATLABTable
object. The DOM
representation of a MATLAB table has the structure of a DOM formal table. See Create Formal Tables. The
MATLABTable
header contains the column names from the MATLAB table. The MATLABTable
body contains the rows and elements from
the MATLAB table.
If the table in MATLAB has row names, the first column of the MATLABTable
object
contains the row names. In MATLAB, the row name column does not have a heading. You can specify a heading for
the row name column by changing the content of the first element of the header in the
MATLABTable
object. You can specify a line under the row name heading by
setting the RowNamesRule
property of the MATLABTable
object.
For information about other types of tables that you can create in reports, see Choose Type of Table to Create.
This example shows how to create a DOM Table from a MATLAB table.
Create a MATLAB table. This MATLAB table shows the age, weight, and height of patients. Use the RowNames
option to identify each row by the last name of the patient. The row names are not table entries. They are stored in a property of the table.
LastName = {'Sanchez';'Johnson';'Lee';'Diaz';'Brown'}; Age = [38;43;38;40;49]; Height = [71;69;64;67;64]; Weight = [176;163;131;133;119]; mltable = table(Age,Weight,Height,'RowNames',LastName);
Create an mlreportgen.MATLABTable
object from the MATLAB
table.
import mlreportgen.dom.*
mltableObj = MATLABTable(mltable);
Optionally, specify a header for the column of row names and draw a line under it. Replace the empty text in the first entry of the table header row with the header.
th = mltableObj.Header;
thentry11 = entry(th,1,1);
thentry11.Children(1).Children(1).Content = 'Names';
mltableObj.RowNamesRule = true;
Create a document and append the MATLABTable
object to the document. Close and view the document.
d = Document('MyMATLABTable','pdf'); append(d,mltableObj); close(d); rptview(d);
Here is the table in the generated report:
By default, a table generated from a MATLABTable
object is formatted to
look like a table in MATLAB. To customize the appearance of the table, you can use the same approaches
that you use for other types of tables:
Update the default style in the style sheets of the default HTML, Word, or
PDF template. The default StyleName
of a
MATLABTable
object is
"rgMATLABTable"
.
Set the StyleName
property to a custom style.
Use format properties or format objects with the table or a section of the table.
See Format Tables.
Table
This example shows how to format the sections of a DOM table that is created from a MATLAB table.
Format the Table Header
This example makes the column headings italic.
To access the header, use the Header
property of the mlreportgen.dom.MATLABTable
object. The example adds an mlreportgen.dom.Italic
format object to the Style
property of the mlreportgen.dom.TableHeader
object that represents the header.
import mlreportgen.dom.* d = Document('myMATLABTable','pdf'); Age = [38;43;38;40;49]; Height = [71;69;64;67;64]; Weight = [176;163;131;133;119]; mltable = table(Age,Weight,Height); mltableObj = MATLABTable(mltable); mltableObj.Header.Style = [mltableObj.Header.Style {Italic(true)}]; append(d,mltableObj); close(d); rptview(d);
Format the Table Body
This example makes the entries of the first row of the table body blue.
To access the table body, use the Body
property of the mlreportgen.dom.MATLABTable
object. To access a row of the body, use the row
method of the mlreportgen.dom.TableRow
object that represents the row. The example adds an mlreportgen.dom.Color
format object to the Style
property of the TableRow
object that represents the first row.
import mlreportgen.dom.* d = Document('myMATLABTable','pdf'); Age = [38;43;38;40;49]; Height = [71;69;64;67;64]; Weight = [176;163;131;133;119]; mltable = table(Age,Weight,Height); mltableObj = MATLABTable(mltable); tbody = mltableObj.Body; row1 = row(tbody,1); row1.Style = [row1.Style {Color('blue')}]; append(d,mltableObj); close(d); rptview(d);