Этот пример показывает, как вычислить количество строк в исходном коде и сгенерированном коде при помощи информационного объекта отчета. Для получения дополнительной информации об информационном объекте отчета смотрите, что Генерация Кода доступа Сообщает информацию Программно.
Добавьте файлы в качестве примера в путь.
path = fullfile(matlabroot, 'examples', 'coder', 'main'); addpath(path);
В этом примере вы генерируете код для функции 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, включайте опцию -reportinfo с именем переменной при выполнении codegen команды. В этом примере вы экспортируете генерацию кода, сообщают информацию переменной info.
codegen -c dijkstra -args {A, p} -reportinfo info
Функция loc берет информационный объект отчета, как введено и возвращает два выходных параметра, которые содержат количество строк в исходном коде и сгенерированном коде, соответственно. Эта функция исключает пустые строки и строки, содержащие комментарии при вычислении количества строк кода.
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
Вызовите loc с информационным объектом отчета info, как введено. Отобразите количество строк кода в исходных файлах и сгенерированных файлах.
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): 582
Удалите файлы в качестве примера из пути.
rmpath(path);