Main Content

Export Polyspace Analysis Results to Excel by Using MATLAB Scripts

You can export the results of a Bug Finder or Code Prover analysis to an Excel® report. See Export Polyspace Analysis Results. The report contains Polyspace® results in a tab-delimited text file with predefined content and formatting.

You can also create Excel reports with your own content and formatting. Automate the creation of this report by using MATLAB® scripts.

Report Result Summary and Details in One Worksheet

This example shows a sample script for generating Excel reports from Polyspace results.

The script adds two worksheets to an Excel workbook. The worksheets report content from the Polyspace results in polyspaceroot\polyspace\examples\cxx\Code_Prover_Example\Module_1\CP_result. Here, polyspaceroot is the Polyspace installation folder, such as C:\Program Files\Polyspace\R2019a.

Each worksheet contains the summary and details for a specific type of Polyspace result:

  • MISRA C:2012: This worksheet contains a summary of MISRA C™: 2012 rule violations in the Polyspace results. The summary is followed by details of each MISRA C: 2012 violation.

  • RTE: This worksheet contains a summary of run-time errors that Code Prover found. The summary is followed by details of each run-time error.

% 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.
results = polyspace.CodeProverResults(userResPath);
resultsTable = results.getResults;

% Delete any existing file and create new file
filename = 'polyspace.xlsx';
if isfile(filename) 
    delete(filename)
end

% Disable warnings about adding new worksheets
warning('off','MATLAB:xlswrite:AddSheet')

% Write MISRA summary to the MISRA 2012 worksheet
misraSummaryTable = results.getSummary('misraC2012');
writetable(misraSummaryTable, filename, 'Sheet', 'MISRA 2012');

% Write MISRA results to the MISRA 2012 worksheet
misraDetailsTable = resultsTable(resultsTable.Family == 'MISRA C:2012',:);
detailsStartingCell = strcat('A',num2str(height(misraSummaryTable)+ 4)); 
writetable(misraDetailsTable, filename, 'Sheet', 'MISRA 2012', 'Range', ...
detailsStartingCell);

% Write runtime summary to the RTE worksheet
rteSummaryTable = results.getSummary('runtime');
writetable(rteSummaryTable, filename, 'Sheet', 'RTE');

% Write runtime results to the RTE worksheet
rteResultsTable = resultsTable(resultsTable.Family == 'Run-time Check',:);
detailsStartingCell = strcat('A',num2str(height(rteSummaryTable)+ 4));
writetable(rteResultsTable, filename, 'Sheet', 'RTE', 'Range', detailsStartingCell);

The key functions used in the example are:

  • polyspace.CodeProverResults: Read Code Prover results into a table.

  • writetable: Write a MATLAB table to a file. If the file name has the extension .xslx, the function writes to an Excel file.

    To specify the content to write to the Excel sheet, use these name-value pairs:

    • Use the name Sheet paired with a sheet name to specify a worksheet in the Excel workbook.

    • Use the name Range paired with a cell name to specify the starting cell in the worksheet where the writing begins.

Control Formatting of Excel Report

Though you can control the content exported to the Excel report by using the preceding method, the method has limited formatting options for the report.

To format the Excel report on Windows® systems, access the COM server directly by using actxserver. For example, Technical Solution 1-QLD4K uses actxserver to establish a connection between MATLAB® and Excel, write data to a worksheet, and specify the colors of the cells.

See also Get Started with COM.

Related Topics