Add Report Content Using Components

Report Components

Report components specify the information to include in the report. The following figure shows a sample page from the report that you create in this example, highlighting components that you use to produce the report.

Specify Report Variables

The magic squares report uses variables defined in the MATLAB® workspace to specify the number and sizes of squares to display and whether to display the variables as tables of numbers or images of color-coded squares:

  • The magicSizeVector variable specifies an array of magic square sizes

  • largestDisplayedArray variable specifies the size of the largest magic square to be displayed as an array of numbers

You could require that a user create these variables in the MATLAB workspace before running the report. However, a better solution is to let the report itself create the variables, using the Evaluate MATLAB Expression component.

To use the Evaluate MATLAB Expression component to define the report variables.

  1. In the Outline pane on the left, select the root component of the report setup.

  2. In the Library pane in the middle, under the MATLAB category, select Evaluate MATLAB Expression.

  3. In the Properties pane on the right, click the icon next to Add component to current report to insert the Evaluate MATLAB Expression component into the report.

    You cannot edit the component information in the Properties pane until you have added the component to the report.

    In the Outline pane, the Eval component appears under the magic_squares report.

    The icon in the upper left corner of the Eval component indicates that this component cannot have child components. By default, any components you add with the Eval component selected are siblings to this component.

    The options for the Evaluate MATLAB Expression component appear in the Properties pane.

  4. To exclude the MATLAB code details and its output in this report, clear the Insert MATLAB expression in report and Display command window output in report check boxes.

  5. In the Expression to evaluate in the base workspace text box, replace the existing text with the following MATLAB code.

      Tip   Copy and paste this text from the HTML documentation into the Report Explorer.

    %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;
  6. In the Evaluate this expression if there is an error text box, replace the existing text with the following text.

    disp(['Error during eval: ', evalException.message])

    This causes an error to display if the MATLAB code fails.

      Tip   To execute these commands immediately, in the top right corner of the Report Explorer, click the Eval Now button. This confirms that your commands are correct, to reduce the chances of report generation problems.

  7. Save the report. Select File > Save.

Create a Title Page

To create a title page for the report, use the Title Page component.

  1. In the Outline pane on the left, select the Eval component.

  2. In the Options pane in the middle, under the Formatting category, double-click Title Page to add the component to the report.

    Because the Eval component icon indicates that this component cannot have children, the Title Page component is a sibling of the Eval component. Likewise, the Title Page component also cannot have child components.

      Note:   To use a Title Page component, you need to have a Chapter component in your report. You have not yet added a Chapter component, so the Properties pane displays a message indicating that a chapters is required for the Title Page component to appear correctly. Because later in this example you add Chapter components to this report, you can ignore that message.

  3. In the Properties pane on the right, use the Main tab to enter the following title page information.

    1. In the Title text box, enter Magic Squares.

    2. In the Subtitle text box, enter Columns, Rows, Diagonals: Everyone is Equal.

    3. Under Options, choose Custom author from the selection list.

    4. In the field to the right of the Custom author field, enter Albrecht Durer.

      Albrecht Dürer created an etching that contains a magic square. Your final report includes an image of that etching.

    5. Select the Include copyright holder and year check box.

    6. In the next text box, enter The MathWorks.

    7. In the second text box, enter 1988.

  4. In the Properties pane, click the Abstract tab and then enter the following text:

    An introduction to Magic Squares and their meaning.

    The pane should look as follows:

  5. Save the report.

Add a Chapter

Add a chapter to the report by using the Chapter/Subsection component.

  1. In the Outline pane on the left, select the Title Page component.

  2. In the Library pane in the middle, under the Formatting category, double-click Chapter/Subsection.

    The Outline pane looks as follows.

    The Eval, Title Page, and Chapter components are all child components of the report's top level, and are siblings of one another.

    The Chapter component can have child components. The next section explains how to add child components to this Chapter component.

  3. For the custom chapter title, in the Properties pane on the right, enter Magic Squares Explained.

    The Outline pane changes to reflect the chapter title.

  4. Save the report.

Add Introductory Text to the First Chapter

Include introductory text in the first chapter by adding the Paragraph and Text components.

  1. In the Outline pane on the left, select the Chapter component.

  2. In the Library pane in the middle, under the Formatting category, double-click Paragraph.

    In the Outline pane, the new component appears as a child of the Chapter component.

  3. By default, the Paragraph component inherits its text from its child components. Add two Text components.

      Note:   The Text component must have the Paragraph component as its parent.

  4. In the Library pane, under the Formatting category, double-click Text.

  5. Double-click Text again to add a second component.

    The Outline pane looks as follows.

  6. In the Outline pane, select the first Text component.

  7. In the Text to include in report text box, enter %<help('magic')>.

    The % sign and angle brackets <> indicate to the MATLAB Report Generator™ software that this is MATLAB code to evaluate. The command help('magic') displays information about the MATLAB magic function.

  8. In the Outline pane, select the second Text component.

  9. In the Text to include in report text box, enter the following text.

      Tip   Copy and paste this text from the HTML documentation into the Report Explorer.

    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.  
  10. Save the report.

The contents of the first chapter are now complete.

Add an Image

The next steps create an image of Albrecht Dürer and include it in the report.

  1. In the Outline pane on the left, select the Chapter component.

  2. In the Library pane in the middle, under the MATLAB category, double-click Evaluate MATLAB Expression.

    The new component becomes a child of the Chapter component.

  3. Move the Eval component under the Paragraph component so that the image follows the introductory text by clicking the down arrow on the toolbar.

  4. With the Eval component still selected, do the following in the Properties pane on the right:

    1. Clear the Insert MATLAB expression in report and Display command window output in report check boxes. You do not want to include the code or its output in the report.

    2. In the Expression to evaluate in the base workspace text box, replace the existing text with the following MATLAB code.

        Tip   Copy and paste this text from the HTML documentation into the Report Explorer.

      %This 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

      This MATLAB code displays the Dürer etching in a MATLAB figure window.

    3. In the Evaluate expression if there is an error text box, replace the existing text with the following text:

      disp(['Error during eval: ', evalException.message])

      This code executes if an error occurs while loading the Dürer etching.

      The Properties pane on the right looks as follows.

  5. In the Outline pane on the left, select the Eval component.

  6. In the Library pane in the middle, under the Handle Graphics category, double-click Figure Snapshot.

    To inline an image component (such as Image or Figure Snapshot), include it within a Paragraph component.

  7. In the Properties pane:

    1. In the Paper orientation list, select Portrait.

    2. In the Invert hardcopy list, select Don't invert.

      Selecting this option specifies not to change the image's on-screen colors for printing.

    The next three steps set up the report to delete the image from the MATLAB workspace after the image has been added to the report.

  8. In the Outline pane, select the Figure Snapshot component.

  9. In the Library pane, under the MATLAB category, double-click Evaluate MATLAB Expression.

  10. In the Properties pane:

    1. Clear the Insert MATLAB expression in report and Display command window output in report check boxes. You do not want to include the code or its output in the report.

    2. In the Expression to evaluate in the base workspace text box, replace the existing text with the following text:

      %This command deletes the Durer image
      delete(gcf);

      The delete(gcf) command deletes the current image in the MATLAB workspace, in this case, the Dürer etching.

    3. In the Evaluate expression if there is an error text box, replace the existing text with the following text:

      disp(['Error during eval: ', evalException.message])

      This code executes if an error occurs while deleting the Dürer etching.

  11. Save the report.

Create the Magic Squares and Their Images

In the next steps, you add a chapter to the report for each magic square specified by the magicSizeVector report variable. You use a For Loop component to perform this essentially repetitive task. To create the magic squares and their images, you perform these tasks:

Create a For Loop

  1. In the Outline pane on the left, select the Chapter component.

  2. In the Library pane in the middle, under the Logical and Flow Control category, double-click For Loop.

    The Outline pane looks as follows.

    This For Loop component appears inside the Chapter component. However, the magic squares should be processed after the first chapter, so the for component should be a sibling of the Chapter component, not a child.

  3. In the Outline pane, select the for component.

  4. Click the left arrow to make the for component a sibling, not a child, of the Chapter component.

  5. In the Properties pane on the right:

    1. In the End text box, replace the existing text with the following text:

      length(magicSizeVector)

      This is the length of the vector that contains the various sizes for the magic square matrices.

    2. In the Variable name text box, replace the existing text with the following text:

      MAGIC_SQUARE_INDEX

      This variable acts as a loop index.

    The Outline pane looks as follows.

  6. Save the report.

Add a Chapter for Each Square

Next create a chapter for each square by adding a Chapter component to the report as a child of the For Loop component. This causes the Report Generator to create a chapter on each iteration of the For Loop during report generation.

  1. In the Outline pane on the left, select the for component.

  2. In the Library pane in the middle, under the Formatting category, double-click Chapter/Subsection.

    It becomes a child of the for component.

  3. In the Properties pane on the right, select Custom from the Title list and enter the following for the chapter title:

    Magic Square # %<MAGIC_SQUARE_INDEX>

    The Properties pane looks as follows.

  4. Save the report.

Determine the Matrix Size

Extract the size of each magic square matrix from magicSizeVector using an Evaluate MATLAB Expression component.

  1. In the Outline pane on the left, select the bottom Chapter component.

  2. In the Library pane in the middle, under the MATLAB category, double-click Evaluate MATLAB Expression.

  3. In the Properties pane on the right:

    1. Clear the Insert MATLAB expression in report and Display command window output in report check boxes.

    2. In the Expression to evaluate in the base workspace text box, replace the existing text with the following text:

      magic_Square_Size=magicSizeVector(MAGIC_SQUARE_INDEX);

      This command extracts the next size for the magic square from the vector of sizes initialized in the first Eval component of the report. The variable magic_Square_Size represents the size of the current magic square being processed.

    3. In the Evaluate expression if there is an error text box, replace the existing text with the following:

      disp(['Error during eval: ', evalException.message])

      This code executes if an error occurs while attempting to extract a value from magicSizeVector.

    The Outline pane looks as follows.

  4. Save the report.

Insert the Magic Square Size into the Report

Insert the size of the magic square into the report using the Paragraph and Insert Variable components.

  1. In the Outline pane on the left, select the bottom Eval component.

  2. In the Library pane in the middle, under the Formatting category, double-click Paragraph.

    Do not change the properties. The variable that contains the size of the magic square goes in this paragraph.

  3. In the Outline pane, select the Paragraph component (below the for component).

  4. In the Library pane, under the MATLAB category, double-click Insert Variable.

  5. In the Properties pane on the right:

    1. In the Variable name text box, enter magic_Square_Size.

    2. In the Display as list, select Inline text.

    The Outline pane looks as follows.

  6. Save the report.

Create the Magic Square

To create the magic square and display the associated matrix or image, use the Evaluate MATLAB Expression component.

  1. In the Outline pane on the left, select the bottom Paragraph component.

  2. In the Library pane in the middle, under the MATLAB category, double-click Evaluate MATLAB Expression.

    Make this component a sibling of the Paragraph component, not a child, as described in the next two steps.

  3. In the Outline pane, select the Eval component.

  4. Click the left arrow on the toolbar to make the Eval component a sibling of the previous Paragraph component.

  5. In the Properties pane on the right:

    1. Clear the Insert MATLAB expression in report and Display command window output in report check boxes.

    2. In the Expression to evaluate in the base workspace text box, replace the existing text with the following MATLAB code.

        Tip   Copy and paste this text from the HTML documentation into the Report Explorer.

      %This MATLAB script 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;
      

      This code creates a magic square matrix mySquare of size magic_Square_Size, and opens an image of that matrix in the MATLAB figure window.

    3. In the Evaluate expression if there is an error text box, replace the existing text with the following:

      disp(['Error during eval: ', evalException.message])

      This code executes if an error occurs while creating and displaying the magic square.

    The Properties pane looks as follows.

  6. Save the report.

Add Display Logic

Use Logical If, Logical Then, and Logical Else components to determine whether to display the magic square as an array of numbers or as an image.

  1. In the Outline pane on the left, select the Eval component.

  2. On the Library pane in the middle, under the Logical and Flow Control category, double-click Logical If.

  3. On the Properties pane on the right, in the Test Expression text box, replace the existing text with the following text:

    magic_Square_Size<=largestDisplayedArray

    This command tests whether the current matrix size (magic_Square_Size) is less than or equal to the value assigned in the first Eval component of the report (largestDisplayedArray=15).

    To process the result of this Logical If component, create two child components—Logical Then and Logical Else. If magic_Square_Size is less than or equal to 15, the matrix variable appears in the report. If magic_Square_Size is greater than 15, the matrix image appears in the report.

  4. On the Outline pane, select the if component.

  5. On the Library pane, under Logical and Flow Control, double-click Logical Else.

  6. On the Outline pane, select the if component again.

  7. On the Library pane, under Logical and Flow Control, double-click Logical Then.

    The then component appears above the else component.

  8. Save the report.

Display the Magic Square

  1. In the Outline pane on the left, select the then component.

  2. In the Library pane in the middle, under the MATLAB category, double-click Insert Variable.

  3. In the Properties pane on the right:

    1. In the Variable name text box, enter mySquare, which is the variable that contains the magic square of the specified size.

    2. In the Title list, select None.

    3. In the Array size limit text box, enter 0.

      The Properties pane on the right looks as follows.

      This Variable component displays the magic square matrix, stored in the mySquare variable.

  4. In the Outline pane, select the else component.

  5. In the Library pane, under the Handle Graphics category, double-click Figure Loop.

    Do not change its properties.

  6. In the Outline pane, select the Figure Loop component.

  7. In the Library pane, under the Handle Graphics category, double-click Figure Snapshot.

  8. In the Properties pane:

    1. In the Paper orientation list, select Portrait.

    2. In the Image size list, select Custom.

    3. Under the Image size list, enter [5 4] for the custom image size.

    4. In the Invert hardcopy list, select Invert.

      This option changes dark axes colors to light axes colors, and vice versa.

      The Properties pane on the right looks as follows.

    The Outline pane looks like the following.

  9. Save the report.

Was this topic helpful?