exponenta event banner

Схемы мозаики Simulink

В этом примере показано, как создать отчет с большой диаграммой, которая охватывает несколько страниц.

Создание отчета с помощью плиток изображений

Откройте модель с большой диаграммой.

model = 'slreportgen_demo_big_diagram';
open_system(model);

Создайте большой файл изображения для разделения на фрагменты.

imgFile = [model '.png'];
print('-dpng', ['-s' model], imgFile);

Создание и открытие отчета.

% To create a Word report, change the output type from "pdf" to "docx". 
% To create an HTML report, change "pdf" to "html" or "html-file for 
% a multifile or single-file report, respectively.
rpt = slreportgen.report.Report('myreport2', 'pdf');
open(rpt);

Получение информации о макете страницы.

pageLayout = rpt.Document.CurrentPageLayout;
pageSize = pageLayout.PageSize;
pageMargins = pageLayout.PageMargins;

Установите для верхнего и нижнего колонтитулов страницы значение 0 дюймов, чтобы увеличить пространство.

pageMargins.Header = '0in';
pageMargins.Footer = '0in';

Определите размер плитки изображения, которая помещается на страницу. Оптимальным размером плитки является размер страницы за вычетом полей страницы, желобов, колонтитулов. Кроме того, отрегулируйте высоту плитки так, чтобы она содержала 0,5 дюйма. Обратите внимание, что для PDF-документов генератор отчетов MATLAB определяет один дюйм равным 96 пикселям.

dpi = 96;
units = mlreportgen.utils.units;

tileHeight = units.toPixels(pageSize.Height, 'Resolution', dpi) ...
    - units.toPixels(pageMargins.Top, 'Resolution', dpi) ...
    - units.toPixels(pageMargins.Bottom, 'Resolution', dpi) ...
    - units.toPixels(pageMargins.Header, 'Resolution', dpi) ...
    - units.toPixels(pageMargins.Footer, 'Resolution', dpi) ...
    - units.toPixels('0.5in', 'Resolution', dpi);

tileWidth = units.toPixels(pageSize.Width, 'Resolution', dpi) ...
    - units.toPixels(pageMargins.Left, 'Resolution', dpi) ...
    - units.toPixels(pageMargins.Right, 'Resolution', dpi) ...
    - units.toPixels(pageMargins.Gutter, 'Resolution', dpi);

tileSize = [tileWidth tileHeight];

Вызовите локальную функцию sliceImage (см. ниже), чтобы разделить большой файл изображения на фрагменты изображения.

tiles = sliceImage(imgFile, [tileWidth tileHeight]);

Добавьте изображения плиток в отчет. Также добавьте подпись, чтобы указать, к чему относится изображение плитки по отношению к общему изображению.

for i = 1:numel(tiles)
    tile = tiles{i};
    formalImage = mlreportgen.report.FormalImage(tile.File);
    formalImage.ScaleToFit = false;
    formalImage.Caption = sprintf('row: %d, col: %d', tile.Row, tile.Col);
    add(rpt, formalImage);
end

Создайте и просмотрите отчет.

close(rpt);
rptview(rpt);

Определение локальной функции sliceImage

Чтобы разделить файл изображения на фрагменты, прочитайте файл изображения и скопируйте элементы размера плитки в несколько файлов изображений.

function tiles = sliceImage(imgFile, tileSize)    
    % Read in the image file and determine the number of row and column 
    % tiles. Note that the image data is row-major, where the rows are 
    % specified first and the columns are second.
    img = imread(imgFile);
    imgSize = size(img); 
    
    imgRows = imgSize(1); % image height
    imgCols = imgSize(2); % image width
    
    tileNumRows = tileSize(2); % tile height
    tileNumCols = tileSize(1); % tile width
    
    numCols = ceil(imgCols / tileNumCols);
    numRows = ceil(imgRows / tileNumRows);    

    % Preallocate the tile data structures.
    tiles = cell(1, numCols*numRows);

    % Determine the base filename to create the tile image filenames.
    [fPath, fName, fExt] = fileparts(imgFile);
    tileName = fullfile(fPath, fName);

    % Iterate through all rows and columns.
    count = 0;
    for rowIdx = 1:numRows
        for colIdx = 1:numCols           
            % Determine the starting and ending image data indices to copy
            % into the tile image.  At the edges, the ending indices are 
            % the number of rows and number of columns.
            rowStart = (rowIdx - 1) * tileNumRows + 1;
            rowEnd = rowStart + tileNumRows - 1;
            
            colStart = (colIdx - 1) * tileNumCols + 1;
            colEnd = colStart + tileNumCols - 1;

            if (rowEnd >= imgRows)
                rowEnd = imgRows;
            end
            nTileRows = rowEnd - rowStart + 1;
            
            if (colEnd >= imgCols)
                colEnd = imgCols;
            end
            nTileCols = colEnd - colStart + 1;                

            % Copy the tile image data onto a white image tile.
            tileImg = uint8(255 * ones(tileNumRows, tileNumCols, 3));
            tileImg(1:nTileRows, 1:nTileCols, :) = img(rowStart:rowEnd,...
                colStart:colEnd, :);            

            % Write out the image tile.
            outFile = sprintf('%s_%d_%d.%s', tileName, rowIdx, colIdx, fExt);
            imwrite(tileImg, outFile);            

            % Create the tile data structure to describe the tile.
            count = count + 1;
            tiles{count} = struct( ...
                'File', outFile, ...
                'Row', rowIdx, ...
                'Col', colIdx);
        end
    end
end