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 Report Generator は 1 インチを 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