Main Content

Run Polyspace on C/C++ Code Generated from MATLAB Code

After generating C/C++ code from MATLAB® code, you can independently check the generated code for:

  • Bugs or defects and coding rule violations: Use Polyspace® Bug Finder™.

  • Run-time errors: Use Polyspace Code Prover™.

Whether you generate code using the MATLAB Coder™ app or the command codegen, you can follow the same workflow for checking the generated code.

Prerequisites

To run this tutorial:

  • You must have an Embedded Coder® license. The MATLAB Coder app does not show options for running Polyspace unless you have an Embedded Coder license.

  • You must be familiar with how to open and use the MATLAB Coder app or the codegen command. Otherwise, see the MATLAB Coder Getting Started.

  • You must link your Polyspace and MATLAB installations. See Integrate Polyspace with MATLAB and Simulink.

Example Files

This tutorial uses the MATLAB Coder example averaging_filter in polyspaceroot\polyspace\examples\doc_cxx\matlab_coder. Here, polyspacroot is the Polyspace installation folder, for instance, C:\Program Files\Polyspace\R2024b. The example shows a Code Prover analysis. You can follow a similar workflow for Bug Finder.

Run Polyspace Analysis

In the MATLAB Coder app, generate code from the M file averaging_filter and analyze the generated code.

  1. To open the MATLAB Coder app, on the MATLAB toolstrip Apps tab, under Code Generation, click the MATLAB Coder app icon.

  2. Generate standalone C/C++ code (a static library, dynamically linked library, or executable program) in the MATLAB Coder app:

    1. On the Select Source Files page, navigate to the folder polyspaceroot\polyspace\examples\doc_cxx\matlab_coder and add the M file averaging_filter. The function averaging_filter is automatically detected as the entry-point function for code generation.

      If the M file is in a read-only location, the app might prompt you to save the underlying MATLAB Coder project in a writable location. Select Save As to save the project in a writable location.

    2. On the Define Input Types page, specify a data type for the input. In this example, the function averaging_filter has exactly one input. You can select Let me enter input or global types directly and enter a 1 X 100 vector of type double

    3. You can skip the Check for Run-Time Issues page (in a regular workflow, you would provide a test file, generate a MEX file and check for run-time issues).

    4. On the Generate Code page, select the following:

      • Build type: Source Code

      • Language: C

      • Hardware Board Device: MATLAB Host Computer

      • Toolchain: Automatically locate an installed toolchain

    5. Click the Generate button.

  3. After code generation, to analyze the generated code, click on Polyspace in the upper right of the page. On the Polyspace page, click Run.

    A snapshot of the Polyspace pane in the MATLAB Coder App. The basic options that you have to specify on this pane are Product mode, Results type and Output folder.

    If the analysis is completed without errors, the Polyspace results open automatically.

    If you close the results, you can reopen them from the final page in the app, under the section Generated Output. The results are stored in a subfolder results_averaging_filter in the folder containing the MATLAB Coder project.

To script the preceding workflow, run:

% Generate code
matlabFileName = fullfile(polyspaceroot, 'polyspace',...
    'examples','doc_cxx','matlab_coder','averaging_filter.m');
codegenFolder = fullfile(pwd, 'codegenFolder');
codegen(matlabFileName, '-config:lib', '-c', '-args', ...
   {zeros(1,100,'double')}, '-d', codegenFolder);

% Configure Polyspace analysis
opts = pslinkoptions('ec');
opts.ResultDir = [tempdir 'results'];
opts.OpenProjectManager = 1;

% Run Polyspace
[polyspaceFolder, resultsFolder] = pslinkrun('-codegenfolder', codegenFolder, opts);

See Also

Related Topics