www.gusucode.com > rptgen 案例源码程序 matlab代码 > rptgen/rptmagic.m

    function rptmagic(ranks, type, showReport, showStatus)
%RPTMAGIC Generate a magic-squares report
% RPTMAGIC() generates an HTML report on magic squares of size
% 10, 20, 40, 75.
%
% RPTMAGIC(RANKS) generates an HTML report on magic squares specified
% by an array of magic square sizes.
%
% RPTMAGIC(RANKS, TYPE, DISPLAY) displays the report if DISPLAY is
% true.
%
% RPTMAGIC(RANKS, TYPE, DISPLAY, STATUS) displays status messages
% generated by report if STATUS is true.
%
%   
%  This function reproduces the MATLAB Report Generator's magic-square.rpt
%  report example. It produces a report on a user-specifiable set of
%  magic squares, i.e., square arrays whose rows and columns and
%  diagonals add up to the same value. This report consists of an
%  introduction to magic squares and a chapter for each square
%  specifiedby the user. Each chapter displays a
%  magic square as a table or a color-coded image, depending on the
%  size of the square.
%
%  This function relies on a template to generate a report. The
%  template contains the following sections
%
%      * Title page with a hole for today's date
%      * A table of contents page that automatically generates a
%        TOC for the report
%      * An introduction to magic squares followed by a hole for
%        magic square chapters
%
%  This function loops through the holes in the template filling in the
%  date and magic square chapters to generate the report. 

%   Copyright MathWorks, 2013-2014.

switch nargin
    case 0
        ranks = [10, 20, 40, 75];
        type = 'html';
        showReport = false;
        showStatus = false;
    case 1
        type = 'html';
        showReport = false;
        showStatus = false;
    case 2
        type = lower(type);
        showReport = false;
        showStatus = false;
    case 3
        showStatus = false;
end

% This statement eliminates the need to qualify the names of DOM
% objects in this function, e.g., you can refer to 
% mlreportgen.dom.Document simply as Document.
import mlreportgen.dom.*;

% The following lines register a listener for DOM API status
if showStatus
    dispatcher = mlreportgen.dom.MessageDispatcher.getTheDispatcher;
    addlistener(dispatcher, ...
        'Message', ...
        @(src,data) disp(evtdata.Message.formatAsText));
end

% Get location of magic square master template.
%
% Note that there are two versions of the template, an HTML version and
% a Word version. If we do not specify the template extensions, the DOM
% API will pick the correct version, depending on the output document
% type.

m_file_dir_rptmagic = mfilename('fullpath');
% mfilename('fullpath') returns a concatenation of current m-file
% directory, file separator and current m-file name without extention. As
% path to template starts from the current m-file directory, we have to
% call fileparts() once to get rid of the file separator and current m-file
% name. This is a workaround for the fact that fullfile no longer 
% resolves dots (e.g., ../) in paths.
[m_file_dir_rptmagic, ~, ~] = fileparts(m_file_dir_rptmagic);
templatePath =  fullfile(m_file_dir_rptmagic, ...
    'resources/templates/magic/magic_squares_main_template');

reportPath = 'magic';

% Construct a document to generate the report.
rpt = Document(reportPath, type, templatePath);

% Loop through the holes in the report template, filling each.
% Note that using a switch statement embedded in a while loop
% makes this code impervious to changes in the number, order, and
% type of holes in the template. For example, using this construct
% would allow you to rearrange holes in the template or have different
% sets of holes for Word and HTML.
%
% Note also that you can achieve the same end with less code by
% subclassing Document class with methods for filling the holes and
% relying on Document class's fill method to invoke the hole-filling
% methods in the correct order.
while ~strcmp(rpt.CurrentHoleId, '#end#')
    switch rpt.CurrentHoleId
        case 'ReportDate'
            append(rpt, date);
        case 'SquareChapters'
            for i = 1:length(ranks)
                chapter = genChapter(rpt, ranks(i));
                append(rpt, chapter);
            end
    end
    moveToNextHole(rpt);
end

close(rpt);

if showReport
    rptview(reportPath, type);
end


    function chapter = genChapter(rpt, rank)
        % This function create a magic squares chapter by filling holes
        % in a template that specifies the fixed content and layout
        % of the chapter. Specifically the chapter template contains
        % an automatically numbered chapter heading prefix (Chapter N.)
        % with a hole for filling in the chapter title. Following the
        % heading is a hole for a depiction of a magic square as 
        % a table or a color-coded image, depending on the square's
        % size.
        import mlreportgen.dom.*;
        
        % Determine the path of the chapter template.
        
        m_file_dir = mfilename('fullpath');
        [m_file_dir, ~, ~] = fileparts(m_file_dir);
        templatePath =  fullfile(m_file_dir, ...
            'resources/templates/magic/magic_squares_chapter_template');
        
        % Create a document part based on the chapter template. A 
        % document part is basically a subdocument that can be appended to
        % a document or another document part. It allows you to modularize
        % your report generation application.
        chapter = DocumentPart(rpt.Type, templatePath);
        
        % Loop through the holes in the template.
        while ~strcmp(chapter.CurrentHoleId, '#end#')
            switch chapter.CurrentHoleId
                case 'ChapterTitle'
                    append(chapter, sprintf('Rank %d Magic Square', rank));
                case 'MagicSquare'
                    % Compute the magic square of the specified rank, using
                    % MATLAB's magic function.
                    square = magic(rank);
                    
                    % If the square's rank is less than 25, assume that it
                    % will fit onto a page as a table; otherwise, render it
                    % as an image.
                    if rank < 25
                        % Append the magic square as a table to the
                        % chapter. Note that the Table constructor accepts
                        % an MxN numeric array as an argument and converts
                        % it to a table. It also accepts the name of a
                        % table style defined in the template.
                        table = Table(square, 'MagicSquare');
                        % set row and column separators
                        table.Style = { ...
                            RowSep('solid', 'black', '1px'), ...
                            ColSep('solid', 'black', '1px'), };
                        % set the border
                        table.Border = 'double';
                        % set entries aligment
                        table.TableEntriesStyle = { HAlign('right') };
                        append(chapter, table);                                               
                    else
                        % Clear any prexisting figures.
                        clf;
                        
                        % Display square as a color-coded figure.
                        imagesc(square);
                        title(sprintf('Magic Square N=%i', rank))
                        set(gca,'Ydir','normal');
                        axis equal;
                        axis tight;
                      
                        % Convert the figure to a png image for inclusion
                        % in the chapter.
                        fileName = sprintf('magic%d.png', rank);
                        print(gcf, '-dpng', fileName);
                        image = Image(fileName);
                        
                        % Scale the image to an approximate square.
                        image.Height = '4 in';
                        image.Width = '6 in';
                        append(chapter, image);
                        delete(gcf);
                    end
            end
            chapter.moveToNextHole;
        end
        
        close(chapter);
        
    end

end