レポート情報オブジェクトを使用したコードの行数の計算
この例では、レポート情報オブジェクトを使用してソース コードと生成コードの行数を計算する方法を説明します。レポート情報オブジェクトの詳細については、コード生成レポートの情報へのプログラムによるアクセスを参照してください。
MATLAB コード
この例では、MATLAB 関数 dijkstra
のコードを生成します。この関数は、ダイクストラ アルゴリズムを使用して、グラフ内のあるノードから他のすべてのノードまでの最短経路の長さを計算します。
type dijkstra
% DIJKSTRA Find length of shortest path between nodes in a graph % % D = dijkstra(A, p) % Takes a graph represented by its adjacency matrix 'A' along with a node % 'p' as input and returns a vector 'D' containing the length of the % shortest path from 'p' to all other nodes in the graph. % Copyright 2018 The MathWorks, Inc. function D = dijkstra(A, p) %#codegen narginchk(2,2); [m, n] = size(A); % Assertions to make sure inputs are valid assert(m == n, "Input adjacency matrix for graph must be a square matrix"); assert(rem(p, 1) == 0 && p <= m && p > 0, "Input src must be a node in the graph"); % Initialization max = realmax; D = repmat(max, 1, m); D(p) = 0; visited = false(1, m); for i = 1:m % Select next node to visit min = max; u = -1; for v = 1:n if ~visited(v) && D(v) <= min min = D(v); u = v; end end % Mark selected node as visited visited(u) = true; %{ Update distances of nodes adjacent to selected node that are yet to be visited %} for v = 1:n if(~visited(v) && A(u, v) ~= 0 && D(u) ~= max) distVal = D(u) + A(u, v); if distVal < D(v) D(v) = distVal; end end end end end
グラフの隣接行列 A
と、グラフのトラバーサルを開始するノード p
を指定します。グラフをプロットします。dijkstra
を呼び出し、p
からグラフ内の他のすべてのノードまでの最短距離を計算し、その距離を表示します。
% Sample adjacency Matrix for graph with 5 nodes A = [ 0 1 1 0 0; 1 0 0 1 1; 1 0 0 1 0; 0 1 1 0 1; 0 1 0 1 0 ]; % Plot the graph to see how it looks like G = graph(A, 'omitselfloops'); plot(G, 'EdgeLabel', G.Edges.Weight)
% Source node from where graph traversal begins p = randi(size(A, 1)); % Calculate shortest distance from 'p' to every other node in graph G D = dijkstra(A, p); for i=1:numel(D) fprintf("Length of shortest path from %d to %d is %d. \n", p, i, D(i)); end
Length of shortest path from 5 to 1 is 2. Length of shortest path from 5 to 2 is 1. Length of shortest path from 5 to 3 is 2. Length of shortest path from 5 to 4 is 1. Length of shortest path from 5 to 5 is 0.
コード生成に関する情報のエクスポート
レポート情報オブジェクトは、コード生成に関する情報のプログラムによるアクセスに対応しています。このオブジェクトのプロパティは、コード生成設定、入力ファイル、生成ファイル、コード生成エラー、警告、および情報メッセージに関する情報を提供します。
レポート情報オブジェクトをベース MATLAB ワークスペース内の変数にエクスポートするには、codegen コマンドを実行するときに、変数の名前に -reportinfo
オプションを付けます。この例では、コード生成レポート情報を変数 info
にエクスポートします。
codegen -c dijkstra -args {A, p} -reportinfo info
Code generation successful.
コード行数の計算
関数 loc
は、レポート情報オブジェクトを入力として、ソース コードの行数と生成コードの行数がそれぞれ格納された 2 つの出力を返します。この関数では、コードの行数の計算時に空白行とコメントを含む行を除外します。
type loc
% LOC Calculate total lines of source and generated code in a codegen run % % [i, o] = loc(r) % Takes a report information object 'r' as input, and produces two % outputs - 'i' and 'o' containing the total lines of code in the source % MATLAB files and generated files respectively. % Copyright 2018 The MathWorks, Inc. function [i, o] = loc(r) narginchk(1,1); % Assert that input is a report information object. assert(isa(r, 'coder.ReportInfo'), 'Input must be of type coder.ReportInfo'); % Fetch source and generated files from the report information object. sourceFiles = r.InputFiles; generatedFiles = r.GeneratedFiles; % Count lines of code in source and generated files. Blank lines, and % comments are not counted. i = countLines(sourceFiles, true); o = countLines(generatedFiles, false); end function count = countLines(files, isSource) count = 0; for i=1:numel(files) f = files(i); if isprop(f, 'Text') lines = splitlines(f.Text); for j=1:numel(lines) line = strtrim(lines{j}); if ~isempty(line) && ~isComment(line, isSource) count = count + 1; end end clear isComment; % clear persistent variables end end end function result = isComment(line, isSource) persistent inBlockComment; if isempty(inBlockComment) inBlockComment = false; end if isSource result = (startsWith(line, "%") || inBlockComment); if line == "%{" || line == "%}" inBlockComment = (line ~= "%}"); end else result = (startsWith(line, "/") || inBlockComment); if startsWith(line, "/*") || endsWith(line, "*/") inBlockComment = ~endsWith(line, "*/"); end end end
レポート情報オブジェクト info
を入力として、loc
を呼び出します。ソース ファイルと生成ファイルのコードの行数を表示します。
info = evalin('base', 'info'); [nLocIn, nLocOut] = loc(info); fprintf('Lines of code in source MATLAB file(s): %d', nLocIn);
Lines of code in source MATLAB file(s): 29
fprintf('Lines of code in generated file(s): %d', nLocOut);
Lines of code in generated file(s): 588