Visualize Bug Finder Analysis Results in MATLAB
After a Polyspace® 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.BugFinderResults
object associated with the results.
For instance, to read the demo results in the read-only subfolder
polyspace/examples/cxx/Bug_Finder_Example/Module_1/BF_Result
of the MATLAB installation folder, copy the results to a writable folder and read
them:
resPath = fullfile(polyspaceroot, 'polyspace', ... 'examples', 'cxx', 'Bug_Finder_Example', 'Module_1','BF_Result'); userResPath = tempname; copyfile(resPath,userResPath); resObj = polyspace.BugFinderResults(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
(Polyspace Code Prover) 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
After reading the results to a MATLAB table, you can visualize them 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 showing the distribution of defects by defect groups, 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', ... 'Bug_Finder_Example','Module_1','BF_Result'); userResPath = tempname; copyfile(resPath,userResPath); % Read results into a table. resObj = polyspace.BugFinderResults(userResPath); resTable = getResults(resObj); % Eliminate results that are not defects. matches = (resTable.Family == 'Defect'); defectTable = resTable(matches ,:); % Create a pie chart showing distribution of defects. defectGroupList = removecats(defectTable.Group); pieDefects = pie(defectGroupList); labels = get(pieDefects(2:2:end),'String'); set(pieDefects(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,'Bug Finder Defect Distribution Graph')); paragraphText = ['The following graph shows the distribution of ' ... 'defects 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,'Defect Details')); paragraphText = ['The following table shows the defects ' ... 'in your code.']; append(report, Paragraph(paragraphText)); % Add the table of defects to the report. reducedInfoTable = defectTable(:,{'File','Function','Check',... '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);
polyspace.BugFinderResults
: Read Bug Finder results into a table.pie
: Create pie chart from a categorical array. You can alternatively use the functionhistogram
orheatmap
.To create histograms, replace
pie
withhistogram
in the script and remove the pie chart legends.mlreportgen.dom.Document
(MATLAB Report Generator): Create a report object that specifies the report format and where to store the report.append
(MATLAB Report Generator): Append contents to the existing report.
When you execute the script, you see a distribution of defects by defect group. The script also creates an HTML report that contains the graph and table of Polyspace defects.
You can use any criteria to remove rows from the results table before reporting. The preceding example uses the criteria that the result must be from the defect family. See also Bug Finder result families.
matches = (resTable.Family == 'Defect');
defectTable = resTable(matches ,:);
Instead, you can use another criteria. For instance, you can remove results in header files and retain the results from source files only.
sourceExtensions = [".c",".cpp",".cxx"]; fileNameStrings = string(resTable.File); matches = endsWith(fileNameStrings,sourceExtensions); sourceTable = resTable(matches ,:);