Main Content

Visualize Code Prover Analysis Results in MATLAB

After analysis, you can read your results to a MATLAB® table. Using the table, you can generate graphs or statistics about your results. If you have MATLAB Report Generator™, you can include these tables and graphs in a PDF or HTML report.

Export Results to MATLAB Table

To read existing Polyspace® analysis results into a MATLAB table, use a polyspace.CodeProverResults object associated with the results.

For instance, to read the demo results in the read-only subfolder polyspace/examples/cxx/Code_Prover_Example/Module_1/CP_Result of the MATLAB installation folder, copy the results to a writable folder and read them:

resPath = fullfile(polyspaceroot, 'polyspace', ... 
    'examples', 'cxx', 'Code_Prover_Example', 'Module_1','CP_Result');

userResPath = tempname;
copyfile(resPath,userResPath);

resObj = polyspace.CodeProverResults(userResPath);
resSummary = getSummary(resObj);
resTable = getResults(resObj);
resSummary and resTable are two MATLAB tables containing summary and details of the Polyspace results.

Alternatively, you can run a Polyspace analysis on C/C++ source files using a polyspace.Project object. After analysis, the Results property of the object contains the results. See Run Polyspace Analysis by Using MATLAB Scripts.

Generate Graphs from Results and Include in Report

You can visualize the analysis results in the MATLAB table in a convenient format. If you have MATLAB Report Generator, you can create a PDF or HTML report that contains your visualizations.

This example creates a pie chart showing the distribution of red, gray and orange run-time checks by check type, and includes the chart in a report.

%% This example shows how to create a pie chart from your results and append 
% it to a report.

%% Generate Pie Chart from Polyspace Results

% Copy a demo result set to a temporary folder.
resPath = fullfile(polyspaceroot,'polyspace','examples','cxx', ...
    'Code_Prover_Example','Module_1','CP_Result');  
userResPath = tempname;
copyfile(resPath,userResPath);
 
% Read results into a table.
resObj = polyspace.CodeProverResults(userResPath);
resTable = getResults(resObj);
 
% Keep results that are run-time checks and eliminate green checks.
matches = (resTable.Family == 'Run-time Check') &...
    (resTable.Color  ~= 'Green');
checkTable = resTable(matches, :);
 
% Create a pie chart showing distribution of checks.
checkList = removecats(checkTable.Check);
pieChecks = pie(checkList);
labels = get(pieChecks(2:2:end),'String');
set(pieChecks(2:2:end),'String','');
legend(labels,'Location','bestoutside')

% Save the pie chart.
print('file','-dpng');


%% Append Pie Chart to Report 
% Requires MATLAB Report Generator

% Create a report object.
import mlreportgen.dom.*;
report = Document('PolyspaceReport','html');
 
% Add a heading and paragraph to the report.
append(report, Heading(1,'Code Prover Run-Time Errors Graph'));
paragraphText = ['The following graph shows the distribution of ' ...
               'run-time errors in your code.'];
append(report, Paragraph(paragraphText));

% Add the image to the report.
chartObj = Image('file.png');
append(report, chartObj);

% Add another heading and paragraph to the report.
append(report, Heading(1,'Code Prover Run-Time Errors Details'));
paragraphText = ['The following table shows the run-time errors ' ...
               'in your code.'];
append(report, Paragraph(paragraphText));
 
% Add the table of run-time errors to the report.
reducedInfoTable = checkTable(:,{'File','Function','Check','Color',...
    'Status','Severity','Comment'});
reducedInfoTable = sortrows(reducedInfoTable,[1 2]);
tableObj = MATLABTable(reducedInfoTable);
tableObj.Style = {Border('solid','black'),ColSep('solid','black'),...
    RowSep('solid','black')};
append(report, tableObj);
 
% Close and view the report in a browser.
close(report);
rptview(report.OutputPath);
The key functions used in the example are:

When you execute the script, you see a distribution of checks by check type. The script also creates an HTML report that contains the graph and table of Polyspace checks.

Related Topics