Use Report Explorer to Create Reports
This example shows how to create a report by using the Report Explorer. This report explains magic squares, which are matrices whose columns, rows, and diagonals each add up to the same number. For more information, see magic
.
Create Report Setup File
To set up a report, you create a setup file and add components, which are MATLAB® objects that describe the contents of the report. For instance, the completed report in this example uses components to create a title page, chapters, text, and figures.
Tip: To open the completed report setup file for this example, in the MATLAB Command Window, enter:
setedit magic-square.rpt
To create the report setup file:
1. Open the Report Explorer. From the MATLAB Toolstrip, in the Apps tab, in the Database Connectivity and Reporting section, click Report Generator.
2. Select File > New to create a report setup file. The new report setup has the default name Unnamed.rpt
.
3. In the right pane of the Report Explorer, set these properties for the report:
To save the report in the current working folder, set Directory to
Present working directory
.To generate the report output as HTML, set File format to
HTML (from template)
. Using this option adds a table of contents that you can expand and collapse in the report.In the Report description box, enter this text:
This report creates a series of magic squares and displays them as images. A magic square is a matrix in which the columns, rows, and diagonal add up to the same number.
4. To save your report, select File > Save As and name your report setup file magic_square.rpt
. In the left pane, the new file name appears under Report Generator.
Specify Report Variables
To specify the number and size of squares to display, and whether to display the magic squares as arrays of numbers or images of color-coded squares, use an Evaluate MATLAB Expression
component that creates these variables in the MATLAB workspace:
The variable
magicSizeVector
specifies an array of magic square sizes.The variable
largestDisplayedArray
specifies the size of the largest magic square to display as an array of numbers.
To add the component to the setup file:
1. In the left pane of the Report Explorer, select the report setup file.
2. In the middle pane, in the MATLAB section, select Evaluate MATLAB Expression.
3. In the right pane, click Add component to current report. The left pane shows the new component under the magic_squares
report.
4. In the right pane, specify these properties for the Eval component:
To exclude the MATLAB code details and output from the report, clear the Insert MATLAB expression in report and Display command window output in report check boxes.
To define the variables
magicSizeVector
andlargestDisplayedArray
, in the Expression to evaluate in the base workspace box, enter this MATLAB code:
% This MATLAB code sets up two variables % that define how the report runs. % magicSizeVector is a list of MxM % Magic Square sizes to insert into % the report. Note that magic % squares cannot be 2x2. magicSizeVector=[4 8 16 32]; % largestDisplayedArray sets the % limit of array size that will be % inserted into the report with the % Insert Variable component. largestDisplayedArray=15;
To display an error if the commands from the previous step fail, in the Evaluate this expression if there is an error box, enter this code:
disp("Error during eval:"+evalException.message)
Create Title Page
To create a title page for the report, use a Title Page
component:
1. In the left pane, select the Eval component.
2. In the middle pane, in the Formatting section, select the Title Page component. Then, in the right pane, click Add component to current report.
In the left pane, the icon indicates that the Eval component cannot have child components, so the Title Page component is a sibling of the Eval component.
3. In the right pane, in the Main tab, specify these properties for the Title Page component:
Set Title to
Magic Squares
.Set Subtitle to
Columns, Rows, Diagonals: Everyone is Equal
.Under Options, select
Custom author
. Then, in the box to the right, enterAlbrecht Durer
. Albrecht Dürer created an etching that contains a magic square.Clear the Include report creation date checkbox.
Select the Include copyright holder and year check box. Then, in the boxes to the right, enter
The MathWorks
and1988
.
4. In the Abstract tab, under Abstract Text, enter:
An introduction to Magic Squares and their meaning.
Add First Chapter
To add a chapter to the report, use a Chapter/Subsection
component:
1. In the left pane, select the Title Page component.
2. In the middle pane, in the Formatting section, select the Chapter/Subsection component. Then, in the right pane, click Add component to current report. The left pane displays the Eval, Title Page, and Chapter components as siblings of one another.
3. In the right pane, for Title, select Custom
. Then, in the box to the right, enter Magic Squares Explained
.
Add Introductory Text to First Chapter
To include introductory text in the first chapter, add Paragraph
and Text
components:
1. In the left pane, select the Chapter component.
2. In the middle pane, in the Formatting section, add a Paragraph component and two Text components.
In the left pane, the icon indicates that the Chapter and Paragraph components can have child components, so the new Text components are children of the Paragraph component, which is a child of the Chapter component.
3. In the left pane, select the first Text component. Then, in the right pane, in the Text to include in report box, enter:
%<help("magic")>
The delimiters %<
and >
evaluate MATLAB code and insert the result into the report. In this example, the command displays information about the magic
function.
4. In the left pane, select the second Text component. Then, in the right pane, in the Text to include in report box, enter:
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 that 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.
Add Image
To create an image of the etching by Albrecht Dürer and include it in the report, use a Figure Snapshot
component:
1. In the left pane, select the Chapter component.
2. In the middle pane, in the MATLAB section, add an Evaluate MATLAB Expression component. In the left pane, the Eval component appears as a child of the Chapter component, above the Paragraph component.
3. On the toolbar, use the Move Down button to move the Eval component under the Paragraph component, after the two Text components.
4. In the right pane, specify these properties for the Eval component:
Clear the Insert MATLAB expression in report and Display command window output in report check boxes.
In the Expression to evaluate in the base workspace box, enter this MATLAB code, which displays the Dürer etching in a MATLAB figure window:
% This MATLAB code loads a self-portrait of % Albrecht Durer, a German artist. There is % a magic square in the upper right corner % of the image. durerData=load("durer.mat","-mat"); figure(Units="Pixels",... Position=[200 200 size(durerData.X,2)*.5 size(durerData.X,1)*.5]); image(durerData.X); colormap(durerData.map); axis("image"); set(gca,... Xtick=[], ... Ytick=[], ... Units="normal", ... Position=[0 0 1 1]); clear durerData
In the Evaluate this expression if there is an error box, enter this code, which displays an error if the image of the Dürer etching fails to load:
disp("Error during eval:"+evalException.message)
5. In the middle pane, in the Handle Graphics section, add a Figure Snapshot component. Then, in the right pane, specify these properties:
Set Paper orientation to
Portrait
.Set Background colors to
Light graphics theme
.
6. In the middle pane, in the MATLAB section, add an Evaluate MATLAB Expression component. Then, in the right pane, specify these properties:
Clear the Insert MATLAB expression in report and Display command window output in report check boxes.
In the Expression to evaluate in the base workspace box, enter this MATLAB code, which deletes the current image in the MATLAB workspace:
% This command deletes the Durer image
delete(gcf);
In the Evaluate this expression if there is an error box, enter this code, which displays an error if the command from the previous step fails:
disp("Error during eval:"+evalException.message)
Create Chapter for Each Magic Square
To add a chapter to the report for each magic square specified by the magicSizeVector
variable, use a For Loop
component:
1. In the left pane, select the Chapter component.
2. In the middle pane, in the Logical and Flow Control section, add a For Loop component. In the left pane, the For Loop component appears as a child of the Chapter component, above the Paragraph component.
3. On the toolbar, use the Move Left button to make the For component a sibling of the Chapter component.
4. In the right pane, specify these properties for the For component:
Set End to
length(magicSizeVector)
. This expression calculates the length of the vector that contains the various sizes for the magic square matrices.Set Variable name to
MAGIC_SQUARE_INDEX
. This variable acts as a loop index.
5. In the middle pane, in the MATLAB section, add an Evaluate MATLAB Expression component. Then, in the right pane, specify these properties:
Clear the Insert MATLAB expression in report and Display command window output in report check boxes.
In the Expression to evaluate in the base workspace box, enter this MATLAB code, which saves the next size of magic square as the variable
magic_Square_Size
:
Magic_Square_Size = magicSizeVector(MAGIC_SQUARE_INDEX);
In the Evaluate this expression if there is an error box, enter this code, which displays an error if the command from the previous step fails:
disp("Error during eval:"+evalException.message)
6. In the middle pane, in the Formatting section, add a Chapter/Subsection component. Then, in the right pane, for Title, select Automatic
.
7. In the middle pane, in the MATLAB section, add an Insert Variable component into the report. Then, in the right pane, specify these properties:
Set Variable name to M
agic_Square_Size
.Set Display as to
Inline text
.
Display Each Magic Square
The report displays small magic squares arrays of numbers and large magic squares as color-coded images. To determine which method to use, add Logical If
, Logical Then
, and Logical Else
components:
1. In the left pane, select the Variable component.
2. In the middle pane, in the MATLAB section, add an Evaluate MATLAB Expression component. Then, in the right pane, specify these properties:
Clear the Insert MATLAB expression in report and Display command window output in report check boxes.
In the Expression to evaluate in the base workspace box, enter this MATLAB code, which creates a magic square matrix
mySquare
of sizeMagic_Square_Size
and opens an image of that matrix in the MATLAB figure window:
% This MATLAB code produces a magic % square of size Magic_Square_Size % and creates an image of that square. mySquare=magic(Magic_Square_Size); clf imagesc(mySquare); title(sprintf("Magic Square N=%i",Magic_Square_Size)) set(gca,Ydir="normal"); axis equal; axis tight;
In the Evaluate this expression if there is an error box, enter this code, which displays an error if the command from the previous step fails:
disp("Error during eval:"+evalException.message)
3. In the middle pane, in the Logical and Flow Control section, add a Logical If component to determine whether to display the magic square as an array of numbers or as an image. Then, in the right pane, set Test Expression to
Magic_Square_Size<=largestDisplayedArray
4. In the middle pane, in the Logical and Flow Control section, add a Logical Else component.
5. In the left pane, select the if component again. Then, in the middle pane, in the Logical and Flow Control section, add a Logical Then component. In the left pane, the then component appears above the else component.
6. In the left pane, select the then component.
7. In the middle pane, in the MATLAB section, add an Insert Variable component. Then, in the right pane, specify these properties:
Set Variable name to
mySquare
, which is the variable that contains the magic square.Set Title to
None
.Set Array size limit to
0
.
8. In the left pane, select the else component.
9. In the middle pane, in the Handle Graphics section, add a Figure Loop component and a Figure Snapshot component. Then, in the right pane, specify these properties:
Set Paper orientation to
Portrait
.Set Image size to
Custom
. Then, in the box under Image size, enter a custom image size of[5 4]
.Set Background colors to
Light graphics theme
.
10. Save the report.
Generate Report
To generate the report, on the toolbar, click the Report button . While the report generates:
The left pane of the Report Explorer highlights each component of the report setup file as it executes.
The Message List window shows the progress of the report generation. To specify the level of detail you want the Message List window to display, use the drop-down list at the top of the window.
When processing finishes, the MATLAB Web browser opens and displays the HTML file.