main content

tile simulink diagrams -凯发k8网页登录

this example shows how to create a report with a large diagram that spans across multiple pages.

create report with image tiles

open a model with a large diagram.

model = 'slreportgen_demo_big_diagram';
open_system(model);

create large image file to split into tiles.

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

create and open a report.

% 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);

get the page layout information.

pagelayout = rpt.document.currentpagelayout;
pagesize = pagelayout.pagesize;
pagemargins = pagelayout.pagemargins;

set the page header and footer to 0 inches to maximize space.

pagemargins.header = '0in';
pagemargins.footer = '0in';

determine the image tile size that fits onto the page. the optimal tile size is the page size minus the page margins, gutters, headers and footers. also, adjust the tile height to allow 0.5 inches for the caption. note that for pdf documents, matlab report generator defines one inch as equal to 96 pixels.

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];

call the sliceimage local function (see below) to slice the large image file into image tiles.

tiles = sliceimage(imgfile, [tilewidth tileheight]);

add the tile images to the report. also, add a caption to indicate where the tile image belongs in relation to the overall image.

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

generate and display the report.

close(rpt);
rptview(rpt);

define sliceimage local function

to slice an image file into tiles, read in the image file and copy tile-size parts into multiple image files.

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
网站地图