Create a Report Generator
This example shows how to create a simple report that explains and illustrates magic
squares – matrices whose columns, rows, and diagonals each add up to the same number.
See magic
.
Note
The complete example code is included after the step-by-step instructions.
Run the following command to access the supporting files used in this example.
openExample('rptgen/MatlabReportGeneratorSupportFilesExample');
Import the base classes.
To eliminate the need to use fully qualified names of Report and DOM API objects, use these statements. For example, instead of using
mlreportgen.report.Report
, you can useReport
.import mlreportgen.report.* import mlreportgen.dom.*
Create a report object.
Create the report object. Use
'magic'
as its file name and'html'
as its report type.rpt = Report('magic','html');
To customize properties that apply to the whole report, see
mlreportgen.report.Report
.Add a title page.
Create a title page and specify its title, subtitle and author. Then, add the title page to the report.
tp = TitlePage; tp.Title = 'Magic Squares'; tp.Subtitle = 'Columns, Rows, Diagonals: All Equal Sums'; tp.Author = 'Albrecht Durer'; append(rpt,tp);
Note
If you are using MATLAB® version R2020a or older, replace the
append
function withadd
.To customize additional title page properties, see
mlreportgen.report.TitlePage
.Add a table of contents.
Add a default table of contents object to the report.
append(rpt,TableOfContents);
Note
If you are using MATLAB version R2020a or older, replace the
append
function withadd
.To customize the table of contents, see
mlreportgen.report.TableOfContents
.Add a chapter and chapter sections.
Create a chapter object for the introduction and specify the chapter title. Add a section, add a paragraph to that section, and add that section to the chapter. Create another section and add a paragraph to it.
ch1 = Chapter; ch1.Title = 'Introduction'; sec1 = Section; sec1.Title = 'What is a Magic Square?'; para = Paragraph(['A magic square is an N-by-N matrix '... 'constructed from the integers 1 through N^2 '... 'with equal row, column, and diagonal sums.']); append(sec1,para) append(ch1,sec1) sec2 = Section; sec2.Title = 'Albrecht Durer and the Magic Square'; para = Paragraph([ ... 'The German artist Albrecht Durer (1471-1528) created '... 'many woodcuts and prints with religious and '... 'scientific symbolism. One of his most famous works, '... 'Melancholia I, explores the depressed state of mind '... 'which opposes inspiration and expression. '... 'Renaissance astrologers believed that the Jupiter '... 'magic square (shown in the upper right portion of '... 'the image) could aid in the cure of melancholy. The '... 'engraving''s date (1514) can be found in the '... 'lower row of numbers in the square.']); append(sec2,para) append(ch1,sec2)
Note
If you are using MATLAB version R2020a or older, replace the
append
function withadd
.For information on customizing chapters and sections, see
mlreportgen.report.Chapter
andmlreportgen.report.Section
respectively.Add a figure.
Create an image of Durer in a figure window. Create the image in a MATLAB figure. Add the figure to the second section of introduction chapter and then, add the chapter to the report.
durerImage=load(which('durer.mat'),'-mat'); figure('Units','Pixels','Position',... [200 200 size(durerImage.X,2)*.5 ... size(durerImage.X,1)*.5 ]); image(durerImage.X); colormap(durerImage.map); axis('image'); set(gca,'Xtick',[],'Ytick',[],... 'Units','normal','Position',[0 0 1 1]); append(sec2,Figure) append(rpt,ch1) close gcf
Note
If you are using MATLAB version R2020a or older, replace the
append
function withadd
.For more information on figures, see
mlreportgen.report.Figure
. For more information on images, seemlreportgen.report.FormalImage
.Add a table.
Add another chapter object and specify its title. Specify the MATLAB code to create a 10-by-10 magic square. Add the results to a table and set these table properties:
Row and column separators
Table border
Alignment of table entries
Then, add the table to the chapter and the chapter to the report.
ch2 = Chapter(); ch2.Title = sprintf('10 x 10 Magic Square'); square = magic(10); tbl = Table(square); tbl.Style = {... RowSep('solid','black','1px'),... ColSep('solid','black','1px'),}; tbl.Border = 'double'; tbl.TableEntriesStyle = {HAlign('center')}; append(ch2,tbl); append(rpt,ch2);
Note
If you are using MATLAB version R2020a or older, replace the
append
function withadd
.For more information on tables, see
mlreportgen.dom.Table
.Add a MATLAB figure to a chapter.
Add another chapter object and specify its title. Specify the MATLAB code to create a 25-by-25 magic square and a color-coded figure of the magic square. Then, create a figure object and set its height, width, and caption. Add the figure to the chapter and the chapter to the report.
ch3 = Chapter(); ch3.Title = sprintf('25 x 25 Magic Square'); square = magic(25); clf; imagesc(square) set(gca,'Ydir','normal') axis equal axis tight fig = Figure(gcf); fig.Snapshot.Height = '4in'; fig.Snapshot.Width = '6in'; fig.Snapshot.Caption = sprintf('25 x 25 Magic Square'); append(ch3,fig); append(rpt,ch3); delete(gcf)
Note
If you are using MATLAB version R2020a or older, replace the
append
function withadd
.For more information on figures, see
mlreportgen.report.Figure
.Close and run the report.
close(rpt) rptview(rpt)
The complete code is:
Run the following command to access the supporting files used in this example.
openExample('rptgen/MatlabReportGeneratorSupportFilesExample');
import mlreportgen.report.* import mlreportgen.dom.* rpt = Report('magic','html'); tp = TitlePage; tp.Title = 'Magic Squares'; tp.Subtitle = 'Columns, Rows, Diagonals: All Equal Sums'; tp.Author = 'Albrecht Durer'; append(rpt,tp); append(rpt,TableOfContents); ch1 = Chapter; ch1.Title = 'Introduction'; sec1 = Section; sec1.Title = 'What is a Magic Square?'; para = Paragraph(['A magic square is an N-by-N matrix '... 'constructed from the integers 1 through N^2 '... 'with equal row, column, and diagonal sums.']); append(sec1,para) append(ch1,sec1) sec2=Section; sec2.Title = 'Albrecht Durer and the Magic Square'; para = Paragraph([ ... 'The German artist Albrecht Durer (1471-1528) created '... 'many woodcuts and prints with religious and '... 'scientific symbolism. One of his most famous works, '... 'Melancholia I, explores the depressed state of mind '... 'which opposes inspiration and expression. '... 'Renaissance astrologers believed that the Jupiter '... 'magic square (shown in the upper right portion of '... 'the image) could aid in the cure of melancholy. The '... 'engraving''s date (1514) can be found in the '... 'lower row of numbers in the square.']); append(sec2,para) append(ch1,sec2) durerImage=load(which('durer.mat'),'-mat'); figure('Units','Pixels','Position',... [200 200 size(durerImage.X,2)*.5 ... size(durerImage.X,1)*.5 ]); image(durerImage.X); colormap(durerImage.map); axis('image'); set(gca,'Xtick',[],'Ytick',[],... 'Units','normal','Position',[0 0 1 1]); append(sec2,Figure) append(rpt,ch1) close gcf ch2 = Chapter(); ch2.Title = sprintf('10 x 10 Magic Square'); square = magic(10); tbl = Table(square); tbl.Style = {... RowSep('solid','black','1px'),... ColSep('solid','black','1px'),}; tbl.Border = 'double'; tbl.TableEntriesStyle = {HAlign('center')}; append(ch2,tbl); append(rpt,ch2); ch3 = Chapter(); ch3.Title = sprintf('25 x 25 Magic Square'); square = magic(25); clf; imagesc(square) set(gca,'Ydir','normal') axis equal axis tight fig = Figure(gcf); fig.Snapshot.Height = '4in'; fig.Snapshot.Width = '6in'; fig.Snapshot.Caption = sprintf('25 x 25 Magic Square'); append(ch3,fig); append(rpt,ch3); delete(gcf) close(rpt) rptview(rpt)
Note
If you are using MATLAB version R2020a or older, replace the append
function with
add
.